Skip to content

Commit 7fcd100

Browse files
committed
refactor: jsx slot inference
1 parent e0fca65 commit 7fcd100

File tree

4 files changed

+46
-18
lines changed

4 files changed

+46
-18
lines changed

.changeset/modern-hands-behave.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
'@pandacss/core': patch
3+
---
4+
5+
Fix issue where Panda eagerly tracks every JSX slot of a slot recipe when scanning for recipe props.
6+
7+
For example, assume you have a tabs recipe with the following slots:
8+
9+
```jsx
10+
<Tabs.Root>
11+
<Tabs.List>
12+
<Tabs.Trigger />
13+
</Tabs.List>
14+
<Tabs.Content />
15+
</Tabs.Root>
16+
```
17+
18+
Panda tracks recipe props in `Tabs.Root`, `Tabs.List`, `Tabs.Trigger`, and `Tabs.Content`. This can lead to slightly
19+
more works in the compiler.
20+
21+
This PR fixes this by only tracking recipe props in the `Tabs.Root` slot.

packages/core/__tests__/slot-recipe.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,10 @@ describe('slot recipe ruleset', () => {
103103
"jsx": [
104104
"Checkbox",
105105
"Checkbox.Root",
106-
"Checkbox.Control",
107-
"Checkbox.Label",
106+
"CheckboxRoot",
108107
],
109108
"jsxName": "Checkbox",
110-
"match": /\\^Checkbox\\$\\|\\^Checkbox\\.Root\\$\\|\\^Checkbox\\.Control\\$\\|\\^Checkbox\\.Label\\$/,
109+
"match": /\\^Checkbox\\$\\|\\^Checkbox\\.Root\\$\\|\\^CheckboxRoot\\$/,
111110
"props": [
112111
"size",
113112
],

packages/core/src/recipes.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,22 +121,30 @@ export class Recipes {
121121
sharedState.styles.delete(name)
122122
}
123123

124-
private assignRecipe = (name: string, recipe: RecipeConfig | SlotRecipeConfig) => {
125-
if (recipe.deprecated) this.deprecated.add(name)
126-
127-
const variantKeys = Object.keys(recipe.variants ?? {})
124+
inferJsxSlots = (name: string, recipe: RecipeConfig | SlotRecipeConfig) => {
128125
const capitalized = capitalize(name)
129126
const jsx = Array.from(recipe.jsx ?? [capitalized])
130-
if ('slots' in recipe) {
131-
jsx.push(...recipe.slots.map((slot) => capitalized + '.' + capitalize(slot)))
127+
128+
const ROOT_SLOT = 'root'
129+
130+
if ('slots' in recipe && recipe.slots.includes(ROOT_SLOT)) {
131+
const jsxRootName = capitalize(ROOT_SLOT)
132+
const rootNames: string[] = [`${capitalized}.${jsxRootName}`, `${capitalized}${jsxRootName}`]
133+
jsx.push(...rootNames)
132134
}
133135

134-
const match = createRegex(jsx)
135-
const className = recipe.className ?? name
136+
return jsx
137+
}
138+
139+
private assignRecipe = (name: string, recipe: RecipeConfig | SlotRecipeConfig) => {
140+
if (recipe.deprecated) this.deprecated.add(name)
141+
142+
const variantKeys = Object.keys(recipe.variants ?? {})
143+
const jsx = this.inferJsxSlots(name, recipe)
136144

137145
sharedState.nodes.set(name, {
138146
...this.getNames(name),
139-
className,
147+
className: recipe.className ?? name,
140148
jsx,
141149
type: 'recipe' as const,
142150
variantKeys,
@@ -145,7 +153,7 @@ export class Recipes {
145153
return [key, Object.keys(value)]
146154
}),
147155
),
148-
match,
156+
match: createRegex(jsx),
149157
config: recipe,
150158
splitProps: (props) => {
151159
return splitProps(props, variantKeys) as [Dict, Dict]

packages/parser/__tests__/jsx-recipe.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,35 +212,35 @@ describe('recipe jsx', () => {
212212
{},
213213
],
214214
"name": "Tabs.List",
215-
"type": "jsx-recipe",
215+
"type": "jsx",
216216
},
217217
{
218218
"data": [
219219
{},
220220
],
221221
"name": "Tabs.Trigger",
222-
"type": "jsx-recipe",
222+
"type": "jsx",
223223
},
224224
{
225225
"data": [
226226
{},
227227
],
228228
"name": "Tabs.Trigger",
229-
"type": "jsx-recipe",
229+
"type": "jsx",
230230
},
231231
{
232232
"data": [
233233
{},
234234
],
235235
"name": "Tabs.Trigger",
236-
"type": "jsx-recipe",
236+
"type": "jsx",
237237
},
238238
{
239239
"data": [
240240
{},
241241
],
242242
"name": "Tabs.Indicator",
243-
"type": "jsx-recipe",
243+
"type": "jsx",
244244
},
245245
]
246246
`)

0 commit comments

Comments
 (0)