Add `createVarName` for creating scoped CSS vars
See original GitHub issueI was really looking forward to see what you have been working on and I am super excited to try it out in my project that currently uses Emotion 😃
Currently the createVar
function generates a variable name and then returns it wrapped in var(...)
. This makes it easier to reference a scoped CSS var in style
blocks, but at the same time makes it a bit trickier to set its value outside of a .css.ts
file.
Would you consider adding a new function, for example called createVarName
, that just returns a variable name?
Here is a pseudo-example of usage:
// component.css.ts
import { createVarName, style } from '@vanilla-extract/css';
export const positionVar = createVarName();
export const box = style({
transform: `translate3d(var(${positionVar}), 0, 0)`,
});
// component.ts
import { useRef, useState } from 'react';
import * as styles from './component.css';
export default function SomeComponent() {
const boxRef = useRef<HTMLDivElement>(null);
const [currentIndex, setCurrentIndex] = useState(0);
useLayoutEffect(() => {
if (boxRef.current) {
boxRef.current.style.setProperty(
styles.positionVar,
`${currentIndex * 1000}px`,
);
}
}, [currentIndex]);
return (
<div className={styles.box} ref={boxRef}>
...
</div>
);
}
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Add createVarName for creating scoped CSS vars #2 - GitHub
Currently the createVar function generates a variable name and then returns it wrapped in var(...) . This makes it easier to reference a...
Read more >CSS variables: Scoping - LogRocket Blog
This article shows you how to apply scoping in CSS variables to streamline theming and enable clean, modular design systems.
Read more >Using CSS custom properties (variables) - MDN Web Docs
Custom properties (sometimes referred to as CSS variables or cascading variables) are entities defined by CSS authors that contain specific ...
Read more >Locally Scoped CSS Variables: What, How, and ... - Una Kravets
CSS Custom Properties (also popularly called CSS Variables) are here! This is really exciting because we finally have true variables in CSS!
Read more >How to add/update scoped CSS variables via JavaScript
As suggested by A Haworth and referring Change CSS of class in Javascript? I was able update changeColor function to use CSSStyleSheet(MDN ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@markdalgleish thank you! I think we can close this issue.
@duarten I think your use-case is already catered to by the
createInlineTheme
API?