Skip to content

Commit

Permalink
fix: remove extraneous classes on nested styled composition (#2341)
Browse files Browse the repository at this point in the history
* fix: remove extraneous classes on nested styled composition

* chore: add changeset
  • Loading branch information
astahmer authored Mar 10, 2024
1 parent 39f529e commit 4576a60
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 36 deletions.
28 changes: 28 additions & 0 deletions .changeset/few-dragons-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
'@pandacss/generator': patch
'@pandacss/studio': patch
---

Fix nested `styled` factory composition

```tsx
import { styled } from '../styled-system/jsx'

const BasicBox = styled('div', { base: { fontSize: '10px' } })
const ExtendedBox1 = styled(BasicBox, { base: { fontSize: '20px' } })
const ExtendedBox2 = styled(ExtendedBox1, { base: { fontSize: '30px' } })

export const App = () => {
return (
<>
{/* ✅ fs_10px */}
<BasicBox>text1</BasicBox>
{/* ✅ fs_20px */}
<ExtendedBox1>text2</ExtendedBox1>
{/* BEFORE: ❌ fs_10px fs_30px */}
{/* NOW: ✅ fs_30px */}
<ExtendedBox2>text3</ExtendedBox2>
</>
)
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ export function generatePreactJsxStringLiteralFactory(ctx: Context) {
${ctx.file.import('css, cx', '../css/index')}
function createStyledFn(Dynamic) {
const __base__ = Dynamic.__base__ || Dynamic
return function styledFn(template) {
const styles = css.raw(template)
const ${componentName} = /* @__PURE__ */ forwardRef(function ${componentName}(props, ref) {
const { as: Element = Dynamic.__base__ || Dynamic, ...elementProps } = props
const { as: Element = __base__, ...elementProps } = props
function classes() {
return cx(css(Dynamic.__styles__, styles), elementProps.className)
}
Expand All @@ -29,11 +30,11 @@ export function generatePreactJsxStringLiteralFactory(ctx: Context) {
})
})
const name = getDisplayName(Dynamic)
const name = getDisplayName(__base__)
${componentName}.displayName = \`${factoryName}.\${name}\`
${componentName}.__styles__ = styles
${componentName}.__base__ = Dynamic
${componentName}.__base__ = __base__
return ${componentName}
}
Expand Down
7 changes: 4 additions & 3 deletions packages/generator/src/artifacts/preact-jsx/jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ export function generatePreactJsxFactory(ctx: Context) {
const __cvaFn__ = composeCvaFn(Dynamic.__cva__, cvaFn)
const __shouldForwardProps__ = composeShouldForwardProps(Dynamic, shouldForwardProp)
const __base__ = Dynamic.__base__ || Dynamic
const ${componentName} = /* @__PURE__ */ forwardRef(function ${componentName}(props, ref) {
const { as: Element = Dynamic.__base__ || Dynamic, children, ...restProps } = props
const { as: Element = __base__, children, ...restProps } = props
const combinedProps = useMemo(() => Object.assign({}, defaultProps, restProps), [restProps])
Expand Down Expand Up @@ -64,11 +65,11 @@ export function generatePreactJsxFactory(ctx: Context) {
}, combinedProps.children ?? children)
})
const name = getDisplayName(Dynamic)
const name = getDisplayName(__base__)
${componentName}.displayName = \`${factoryName}.\${name}\`
${componentName}.__cva__ = __cvaFn__
${componentName}.__base__ = Dynamic
${componentName}.__base__ = __base__
${componentName}.__shouldForwardProps__ = shouldForwardProp
return ${componentName}
Expand Down
17 changes: 9 additions & 8 deletions packages/generator/src/artifacts/qwik-jsx/jsx.string-literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ export function generateQwikJsxStringLiteralFactory(ctx: Context) {
${ctx.file.import('css, cx', '../css/index')}
function createStyledFn(Dynamic) {
const __base__ = Dynamic.__base__ || Dynamic
return function styledFn(template) {
const styles = css.raw(template)
const ${componentName} = (props) => {
const { as: Element = Dynamic.__base__ || Dynamic, ...elementProps } = props
const { as: Element = __base__, ...elementProps } = props
function classes() {
return cx(css(Dynamic.__styles__, styles), elementProps.className)
return cx(css(__base__.__styles__, styles), elementProps.className)
}
return h(Element, {
Expand All @@ -27,12 +28,12 @@ export function generateQwikJsxStringLiteralFactory(ctx: Context) {
})
}
const name = getDisplayName(Dynamic)
const name = getDisplayName(__base__)
${componentName}.displayName = \`${factoryName}.\${name}\`
${componentName}.__styles__ = styles
${componentName}.__base__ = Dynamic
${componentName}.__base__ = __base__
return ${componentName}
}
}
Expand Down
7 changes: 4 additions & 3 deletions packages/generator/src/artifacts/qwik-jsx/jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ export function generateQwikJsxFactory(ctx: Context) {
const __cvaFn__ = composeCvaFn(Dynamic.__cva__, cvaFn)
const __shouldForwardProps__ = composeShouldForwardProps(Dynamic, shouldForwardProp)
const __base__ = Dynamic.__base__ || Dynamic
const ${componentName} = function ${componentName}(props) {
const { as: Element = Dynamic.__base__ || Dynamic, children, className, ...restProps } = props
const { as: Element = __base__, children, className, ...restProps } = props
const combinedProps = Object.assign({}, defaultProps, restProps)
Expand Down Expand Up @@ -61,11 +62,11 @@ export function generateQwikJsxFactory(ctx: Context) {
}, combinedProps.children ?? children)
}
const name = getDisplayName(Dynamic)
const name = getDisplayName(__base__)
${componentName}.displayName = \`${factoryName}.\${name}\`
${componentName}.__cva__ = __cvaFn__
${componentName}.__base__ = Dynamic
${componentName}.__base__ = __base__
${componentName}.__shouldForwardProps__ = shouldForwardProp
return ${componentName}
Expand Down
15 changes: 8 additions & 7 deletions packages/generator/src/artifacts/react-jsx/jsx.string-literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ export function generateReactJsxStringLiteralFactory(ctx: Context) {
${ctx.file.import('css, cx', '../css/index')}
function createStyledFn(Dynamic) {
const __base__ = Dynamic.__base__ || Dynamic
return function styledFn(template) {
const styles = css.raw(template)
const ${componentName} = /* @__PURE__ */ forwardRef(function ${componentName}(props, ref) {
const { as: Element = Dynamic.__base__ || Dynamic, ...elementProps } = props
const { as: Element = __base__, ...elementProps } = props
function classes() {
return cx(css(Dynamic.__styles__, styles), elementProps.className)
return cx(css(__base__.__styles__, styles), elementProps.className)
}
return createElement(Element, {
Expand All @@ -28,12 +29,12 @@ export function generateReactJsxStringLiteralFactory(ctx: Context) {
})
})
const name = getDisplayName(Dynamic)
const name = getDisplayName(__base__)
${componentName}.displayName = \`${factoryName}.\${name}\`
${componentName}.__styles__ = styles
${componentName}.__base__ = Dynamic
${componentName}.__base__ = __base__
return ${componentName}
}
}
Expand Down
7 changes: 4 additions & 3 deletions packages/generator/src/artifacts/react-jsx/jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ export function generateReactJsxFactory(ctx: Context) {
const __cvaFn__ = composeCvaFn(Dynamic.__cva__, cvaFn)
const __shouldForwardProps__ = composeShouldForwardProps(Dynamic, shouldForwardProp)
const __base__ = Dynamic.__base__ || Dynamic
const ${componentName} = /* @__PURE__ */ forwardRef(function ${componentName}(props, ref) {
const { as: Element = Dynamic.__base__ || Dynamic, children, ...restProps } = props
const { as: Element = __base__, children, ...restProps } = props
const combinedProps = useMemo(() => Object.assign({}, defaultProps, restProps), [restProps])
Expand Down Expand Up @@ -61,11 +62,11 @@ export function generateReactJsxFactory(ctx: Context) {
}, combinedProps.children ?? children)
})
const name = getDisplayName(Dynamic)
const name = getDisplayName(__base__)
${componentName}.displayName = \`${factoryName}.\${name}\`
${componentName}.__cva__ = __cvaFn__
${componentName}.__base__ = Dynamic
${componentName}.__base__ = __base__
${componentName}.__shouldForwardProps__ = shouldForwardProp
return ${componentName}
Expand Down
7 changes: 4 additions & 3 deletions packages/studio/styled-system/jsx/factory.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ function styledFn(Dynamic, configOrCva = {}, options = {}) {

const __cvaFn__ = composeCvaFn(Dynamic.__cva__, cvaFn)
const __shouldForwardProps__ = composeShouldForwardProps(Dynamic, shouldForwardProp)
const __base__ = Dynamic.__base__ || Dynamic

const PandaComponent = /* @__PURE__ */ forwardRef(function PandaComponent(props, ref) {
const { as: Element = Dynamic.__base__ || Dynamic, children, ...restProps } = props
const { as: Element = __base__, children, ...restProps } = props

const combinedProps = useMemo(() => Object.assign({}, defaultProps, restProps), [restProps])

Expand Down Expand Up @@ -50,11 +51,11 @@ function styledFn(Dynamic, configOrCva = {}, options = {}) {
}, combinedProps.children ?? children)
})

const name = getDisplayName(Dynamic)
const name = getDisplayName(__base__)

PandaComponent.displayName = `panda.${name}`
PandaComponent.__cva__ = __cvaFn__
PandaComponent.__base__ = Dynamic
PandaComponent.__base__ = __base__
PandaComponent.__shouldForwardProps__ = shouldForwardProp

return PandaComponent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ describe('styled factory - cva', () => {
const { firstChild } = container as HTMLElement
expect(firstChild).toMatchInlineSnapshot(`
<button
class="font_semibold h_10 font_semibold h_20 color-palette_red border-w_4px border_currentColor px_12 fs_32px"
class="font_semibold h_20 color-palette_red border-w_4px border_currentColor px_12 fs_32px"
>
Click me
</button>
Expand All @@ -201,7 +201,7 @@ describe('styled factory - cva', () => {
).container.firstChild,
).toMatchInlineSnapshot(`
<button
class="font_semibold h_10 font_semibold h_20 color-palette_blue border-w_4px text_white px_20 fs_40px"
class="font_semibold h_20 color-palette_blue border-w_4px text_white px_20 fs_40px"
>
Click me
</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ describe('styled factory - cva', async () => {
</WithOverrides>,
)
const container = screen.querySelector('button')!
expect(container.outerHTML).toMatchInlineSnapshot(`"<button class="font_semibold h_10 font_semibold h_20 color-palette_red border-w_4px border_currentColor px_12 fs_32px">Click me</button>"`)
expect(container.outerHTML).toMatchInlineSnapshot(`"<button class="font_semibold h_20 color-palette_red border-w_4px border_currentColor px_12 fs_32px">Click me</button>"`)

const second = await createDOM()
await second.render(
Expand All @@ -208,7 +208,7 @@ describe('styled factory - cva', async () => {
</WithOverrides>,
)
const container2 = second.screen.querySelector('button')!
expect(container2.outerHTML).toMatchInlineSnapshot(`"<button class="font_semibold h_10 font_semibold h_20 color-palette_blue border-w_4px text_white px_20 fs_40px">Click me</button>"`)
expect(container2.outerHTML).toMatchInlineSnapshot(`"<button class="font_semibold h_20 color-palette_blue border-w_4px text_white px_20 fs_40px">Click me</button>"`)
})

test('html props', async () => {
Expand Down

0 comments on commit 4576a60

Please sign in to comment.