Skip to content

Commit 717ad60

Browse files
Sprinkles: Add static 'properties' set to atoms function (#81)
1 parent edce7d2 commit 717ad60

File tree

4 files changed

+48
-4
lines changed

4 files changed

+48
-4
lines changed

.changeset/friendly-maps-do.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'@vanilla-extract/sprinkles': minor
3+
---
4+
5+
Add static `properties` set to atoms function
6+
7+
This allows runtime code to detect whether a given property can be handled by the atoms function or not.
8+
9+
This is useful when building a Box component with atoms available at the top level (e.g. `<Box padding="small">`) since you'll need some way to filter atom props from non-atom props.

packages/sprinkles/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,14 @@ export const atoms = createAtomsFn(
476476
);
477477
```
478478

479+
The atoms function also exposes a static `properties` key that lets you check whether a given property can be handled by the function.
480+
481+
```ts
482+
atoms.properties.has('paddingX'); // returns true or false
483+
```
484+
485+
> 💡 This is useful when building a Box component with atoms available at the top level (e.g. `<Box padding="small">`) since you’ll need some way to filter atom props from non-atom props.
486+
479487
---
480488

481489
## Thanks

packages/sprinkles/src/createAtomsFn.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,20 @@ type AtomProps<Args extends ReadonlyArray<any>> = Args extends [
8787
? (L extends AtomicStyles ? ChildAtomProps<L> : never) & AtomProps<R>
8888
: {};
8989

90-
export type AtomsFn<Args extends ReadonlyArray<AtomicStyles>> = (
90+
export type AtomsFn<Args extends ReadonlyArray<AtomicStyles>> = ((
9191
props: AtomProps<Args>,
92-
) => string;
92+
) => string) & { properties: Set<keyof AtomProps<Args>> };
9393

9494
export function createAtomsFn<Args extends ReadonlyArray<AtomicStyles>>(
9595
...args: Args
9696
): AtomsFn<Args> {
9797
const atomicStyles = Object.assign({}, ...args);
98-
const shorthandNames = Object.keys(atomicStyles).filter(
98+
const atomicKeys = Object.keys(atomicStyles) as Array<keyof AtomProps<Args>>;
99+
const shorthandNames = atomicKeys.filter(
99100
(property) => 'mappings' in atomicStyles[property],
100101
);
101102

102-
return (props: any) => {
103+
const atomsFn = (props: any) => {
103104
const classNames = [];
104105
const shorthands: any = {};
105106
const nonShorthands: any = { ...props };
@@ -289,4 +290,8 @@ export function createAtomsFn<Args extends ReadonlyArray<AtomicStyles>>(
289290

290291
return classNames.join(' ');
291292
};
293+
294+
return Object.assign(atomsFn, {
295+
properties: new Set(atomicKeys),
296+
});
292297
}

tests/sprinkles/sprinkles.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,28 @@ describe('sprinkles', () => {
241241
`"sprinkles_paddingTop_large__1kw4bre1z sprinkles_paddingBottom_large__1kw4bre22 sprinkles_paddingLeft_small__1kw4bre1r sprinkles_paddingRight_small__1kw4bre1u"`,
242242
);
243243
});
244+
245+
it('should provide a static set of properties on the atoms fn', () => {
246+
const atoms = createAtomsFn(
247+
atomicWithShorthandStyles,
248+
conditionalAtomicStyles,
249+
);
250+
251+
expect(atoms.properties).toMatchInlineSnapshot(`
252+
Set {
253+
"paddingX",
254+
"anotherPaddingX",
255+
"color",
256+
"paddingLeft",
257+
"paddingRight",
258+
"paddingY",
259+
"display",
260+
"paddingTop",
261+
"paddingBottom",
262+
"opacity",
263+
}
264+
`);
265+
});
244266
});
245267

246268
describe('errors', () => {

0 commit comments

Comments
 (0)