Problem
|
I had an assignment to build a dynamic form in vanilla JavaScript
/ CSS for a landing page. Specifically, I made the label z-indexed
over the textbox field and whenever the user focused on the field, the label
will animate up to the top of the field. To me, this solves the placeholder text
disappearing as soon as the user starts typing then forgets what the field
is. (funny, I know. But it happens to
me all the time).
I ran into a problem where if
the label’s text length is too long, the text would wrap to the next line
obstructing the user. Originally, I
thought I needed a fixed 20px, but I
needed to know the exact height of the label before shooting it up
above the field.
|
Solution
|
Alas, I learned about a JavaScript function called getComputedStyle which solved my problem perfectly. Once I got the computed height of the
label, I made sure the margins were adjusted so the label would clear the
field.
|
Code
|
Here’s an ES6 function you can drop on your class and begin using:
getElementHeight(element){
let calcHeight =
window.getComputedStyle(element, null)
.getPropertyValue("height")
.replace('px',
'');
if(!isNaN(calcHeight)){
calcHeight =
parseInt(calcHeight);
}
return calcHeight;
}
Here’s how I use it:
let calcHeight = this.getElementHeight(myLabel);
myLabel.style.marginTop = ('-' + calcHeight + 'px');
myField.style.marginTop = (calcHeight + 'px');
Essentially, I return the numeric height of the label after a
substring replacement and simple validation.
This will return the height as a number. Of course, you can take the guts of this function and get other style settings, not just height.
|
Summary
|
Ideally, you never want to update your styles directly in the
JavaScript except by adding/removing classes, but sometimes you need to bite
the bullet and use the awesome tools JavaScript provides for us!
|
Resolved Sitecore "If you publish now, the selected version will not be visible on the web site" warning
The Problem: Unable to publish any Sitecore item within a particular site, even out of the workflow. Rather, a warning reads " If you publish now, the selected version will not be visible on the web site " What I've Discovered: I couldn't publish any item in the site, not just one or two I viewed all parent items of the items in question The main home page displayed a different warning " This item will never be published because it's publishable option is disabled " Another sitecore developer reminded me of the standard fields option View --> check standard fields to show all standard page fields Found out that somebody checked Never Publish within the publishing section The Fix: After I unchecked the checkbox and saved the change, I was able to publish again. :) Conclusion: Looks like another Sitecore user thought the children items would not be affected by this change. Lesson learned.
Comments
Post a Comment