Skip to content

Commit

Permalink
feat: support tokens in composite shadows
Browse files Browse the repository at this point in the history
  • Loading branch information
segunadebayo committed Dec 24, 2024
1 parent 7c85ac7 commit ad89b90
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 9 deletions.
32 changes: 32 additions & 0 deletions .changeset/stale-drinks-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
'@pandacss/token-dictionary': minor
'@pandacss/generator': minor
'@pandacss/types': minor
---

Add support for semantic tokens in composite shadow `blur`, `offsetX`, `offsetY` and `spread` properties.

This enables the use of semantic tokens in composite shadow properties.

```ts
// panda.config.ts

export default defineConfig({
theme: {
tokens: {
// ...
shadows: {
sm: {
value: {
offsetX: '{spacing.3}',
offsetY: '{spacing.3}',
blur: '1rem',
spread: '{spacing.3}',
color: '{colors.red}',
},
},
},
},
},
})
```
37 changes: 37 additions & 0 deletions packages/generator/__tests__/generate-token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,43 @@ describe('generator', () => {
`)
})

test('shadow tokens - composite', () => {
const css = tokenCss({
eject: true,
theme: {
tokens: {
spacing: {
'3': { value: '1rem' },
},
colors: {
red: { value: '#ff0000' },
},
shadows: {
sm: {
value: {
offsetX: '{spacing.3}',
offsetY: '{spacing.3}',
blur: '1rem',
spread: '{spacing.3}',
color: '{colors.red}',
},
},
},
},
},
})

expect(css).toMatchInlineSnapshot(`
"@layer tokens {
:where(html) {
--spacing-3: 1rem;
--colors-red: #ff0000;
--shadows-sm: var(--spacing-3) var(--spacing-3) 1rem var(--spacing-3) var(--colors-red);
}
}"
`)
})

test('[css] should generate css', () => {
expect(tokenCss()).toMatchInlineSnapshot(`
"@layer tokens {
Expand Down
8 changes: 4 additions & 4 deletions packages/token-dictionary/src/is-composite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { P, isMatching } from 'ts-pattern'

export const isCompositeShadow = isMatching({
inset: P.optional(P.boolean),
offsetX: P.number,
offsetY: P.number,
blur: P.number,
spread: P.number,
offsetX: P.union(P.number, P.string),
offsetY: P.union(P.number, P.string),
blur: P.union(P.number, P.string),
spread: P.union(P.number, P.string),
color: P.string,
})

Expand Down
6 changes: 5 additions & 1 deletion packages/token-dictionary/src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ function toUnit(v: string | number) {
return isCssUnit(v) || hasReference(v.toString()) ? v : `${v}px`
}

function px(v: string | number) {
return isString(v) ? v : `${v}px`
}

/* -----------------------------------------------------------------------------
* Shadow token transform
* -----------------------------------------------------------------------------*/
Expand All @@ -29,7 +33,7 @@ export const transformShadow: TokenTransformer = {

if (isCompositeShadow(token.value)) {
const { offsetX, offsetY, blur, spread, color, inset } = token.value
return `${inset ? 'inset ' : ''}${offsetX}px ${offsetY}px ${blur}px ${spread}px ${color}`
return [inset ? 'inset ' : '', px(offsetX), px(offsetY), px(blur), px(spread), color].filter(Boolean).join(' ')
}

return token.value
Expand Down
8 changes: 4 additions & 4 deletions packages/types/src/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ export interface Border {
}

export interface Shadow {
offsetX: number
offsetY: number
blur: number
spread: number
offsetX: number | string
offsetY: number | string
blur: number | string
spread: number | string
color: string
inset?: boolean
}
Expand Down

0 comments on commit ad89b90

Please sign in to comment.