Expose utility.transform
to allow className generation to be used outside of Panda
#3241
Replies: 3 comments
-
I don't think you need access to the Here's a quick hack of an approach to consider import { css } from './styled-system/css';
import { serializeStyles } from '@emotion/serialize';
const cmsStyles = [
{ property: 'maxWidth', value: '240px' },
{ property: 'color', value: 'red' },
{ property: 'background', value: 'pink' },
{ property: 'padding', value: '20px' },
];
let styleObject = {};
let styleString = '';
cmsStyles.forEach((entry) => {
const obj: any = { [entry.property]: entry.value };
const name = css(obj);
const { styles } = serializeStyles([css.raw(obj) as any]);
Object.assign(styleObject, obj);
styleString += `\n.${name}{${styles}}`;
});
function App() {
return (
<div>
<h1 className={css(styleObject)}>dsfsdf</h1>
<style>{styleString}</style>
</div>
);
} |
Beta Was this translation helpful? Give feedback.
-
Hey @segunadebayo Thanks for the reply. I can get it working with something like the below
with a
which does return the generated classname and CSS. However, the CMS allows our consumers to change these values responsively, too. I can update the function to be
which does generate the correct string
provided a
but of course this won't add the breakpoints. For reference, this is the code I was working with before, with a
As mentioned in the OP, being able to just generate the className would allow me to remove the need to do something like |
Beta Was this translation helpful? Give feedback.
-
So I made a combination of both that resolves this:
Thanks for your help in pointing me in the right direction! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Howdy
We're integrating Panda in a package we have that interfaces with a CMS. The CMS allows consumers to, for example, set a minimum width on a container. This comes through as a numeric value, which we pass through to a component. Obvious issue here is that although Panda can generate the class name internally, it wont generate any styles as it doesn't know about that value at build time.
To get around this, we've created a context that collects all of these values and injects them as a
style
. The collection looks something likeIdeally I'd like to be able to use your transform function so that I don't need to rely on manually tooling these class names (and means they'll always scale and update in line with the underlying Panda integration)
I did look in
@pandacss/shared
but it doesn't look like this functionality is there.Beta Was this translation helpful? Give feedback.
All reactions