0

I'm writing a react app where I use an scss stylesheet. In this I set width = 240px;, and this is then used in my component. I would like to dynamically update the scss.styles.ActiveWide width attribute depending on how many elements there are in an array:

//.. In function

var betweenTimeIndexes = [];
   for (var i = fromTimeIndex; i <= toTimeIndex; i++) {
     betweenTimeIndexes.push(i);
}
const myCSS = `${(betweenTimeIndexes.length*200)}` // I want my width to be equal to this, and the children of ActiveWide needs to inherit this width.
return(
    <div className={styles.ActiveWide} defaultValue={fromTimeIndex}>

                <div className={styles.ActiveWideTimeSpan}>
                    <div className={styles.ActiveWideFromText}>{fromText}</div>
                    <div className={styles.ActiveWideDash}></div>
                    <div className={styles.ActiveWideToText}>{toText}</div>
                </div>
    </div>
);

My scss class:


.ActiveWide{
    /* State=Active, wide=true,NoOfSlots=? */
    box-sizing: border-box;

    /* Auto layout */

    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 8px 16px;

    width: 240px; // This I want dynamically update
    height: 96px;
    left: 156px;
    top: 136px;

    

    background: rgba(62, 177, 200, 0.5);
    border-bottom: 2px solid #C4D600;
    box-shadow: 0px 4px 4px rgba(0, 0, 0, 0.25);
    border-radius: 8px;
}

I don't know if this approach is correct/possible as when we bundle the application the scss files are converted into static css. If not, how would I do to achieve this?

2
  • You might want to use css variables which can be updated with js. Commented Jul 6, 2022 at 12:21
  • How would I do that? @MatthieuRiegler Commented Jul 6, 2022 at 12:33

1 Answer 1

1

Use inline styling for that.

<div style={{width: 10*betweenTimeIndexes.length + "px" }}>
  ...
</div>
Sign up to request clarification or add additional context in comments.

2 Comments

Will this "overwrite" the width attribute of the .ActiveWide class so that the children of that div also inherits it?
Yes It would, inline styles have higher specificity

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.