question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add `createVarName` for creating scoped CSS vars

See original GitHub issue

I 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:closed
  • Created 2 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
damianstasikcommented, Apr 12, 2021

@markdalgleish thank you! I think we can close this issue.

1reaction
mattcompilescommented, Apr 6, 2021

@duarten I think your use-case is already catered to by the createInlineTheme API?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found