Skip to content

Commit

Permalink
fix(sva): improve infer slots
Browse files Browse the repository at this point in the history
  • Loading branch information
segunadebayo committed Dec 24, 2024
1 parent fea78c7 commit 7c85ac7
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
17 changes: 17 additions & 0 deletions .changeset/tasty-ducks-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
'@pandacss/parser': patch
'@pandacss/core': patch
---

Improve inference of slots in slot recipes when spreading and concatenating slot names.

This handles the following case gracefully:

```ts
const styles = sva({
className: 'foo',
slots: [...componentAnatomy.keys(), 'additional', 'slots', 'here'],
})
```

Panda will now infer the slots from the anatomy and add them to the recipe.
7 changes: 7 additions & 0 deletions packages/core/src/recipes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,13 @@ export class Recipes {
})
})

recipe.compoundVariants?.forEach((compoundVariant) => {
if (!compoundVariant) return
Object.keys(compoundVariant.css ?? {}).forEach((name) => {
slots.add(name)
})
})

return Array.from(slots)
}

Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/style-encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
normalizeStyleObject,
toResponsiveObject,
traverse,
uniq,
} from '@pandacss/shared'
import type {
Dict,
Expand Down Expand Up @@ -268,7 +269,9 @@ export class StyleEncoder {
}

processAtomicSlotRecipe = (recipe: PartialBy<SlotRecipeDefinition, 'slots'>) => {
if (!recipe.slots?.filter(Boolean).length) recipe.slots = Recipes.inferSlots(recipe)
const inferredSlots = Recipes.inferSlots(recipe)
recipe.slots = uniq([...(recipe.slots ?? []), ...inferredSlots].filter(Boolean))

const slots = getSlotRecipes(recipe)

for (const slotRecipe of Object.values(slots)) {
Expand Down
26 changes: 26 additions & 0 deletions packages/parser/__tests__/sva.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,30 @@ describe('ast parser / sva', () => {
}"
`)
})

test('unresolvable + concat - spread', () => {
const code = `
import { anatomy } from '@/slots'
import { sva } from 'styled-system/css'
const card = sva({
slots: [...anatomy().keys(), 'slots', 'here'],
className: 'tt',
base: {
a: {
backgroundColor: 'red',
},
},
})
`

const result = parseAndExtract(code)
expect(result.css).toMatchInlineSnapshot(`
"@layer utilities {
.bg-c_red {
background-color: red;
}
}"
`)
})
})

0 comments on commit 7c85ac7

Please sign in to comment.