My JS code sets a variable on the page:
if (error) {
document.querySelector('body')?.style.setProperty('--pickererror', `'${ error }'`);
}
My SCSS code uses that variable for some content:
$error: var(--pickererror);
// ERROR MESSAGE
.picker-toolbar::before {
content: $error;
color: red;
padding: 5px 35px 0px 35px;
text-align: center;
}
.picker-toolbar {
@if str-length($error) > 0 {
order: 1 !important;
}
}
The ::before section works completely. However the @if clause executes even if there is no error, and the .picker-toolbar is always at order: 1.
I have checked that --pickererror is not present when there's no JS error. I've tried any number of permutations, such as
- Putting the
@ifline before the.picker-toolbarline - Simply using
@if $error - Using
@if var(--pickererror)in place of the$errorvariable.
How do I make this work?
@ifis getting evaluated at runtime when the custom property--pickererrorchanges. I think you'd be better off applying a second class to whatever element has the.picker-toolbarto change the order..picker-xxxelements are managed by a 3rd party component, in my React code. I've been having trouble getting refs which I suppose is what I'd need to add classes and things like that. Sounds like that's the only possible route though?