Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: explicitly setting consumeCss to false should not consume css #2668

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,38 @@ describe("CSS prop forwarding", () => {
`);
});

test("explicitly setting consumeCss to false should not consume the css prop", () => {
const StyledComponent = styled("div", { base: { display: "flex" } });

const { container } = render(<StyledComponent consumeCss={false}>Hello</StyledComponent>);

expect(container.firstChild).toMatchInlineSnapshot(`
<div
class="d_flex"
>
Hello
</div>
`);
});

test("explicitly setting consumeCss and asChild to false should not consume the css prop", () => {
const StyledComponent = styled(ark.div, { base: { display: "flex" } }, { baseComponent: true });

const { container } = render(
<StyledComponent asChild={false} consumeCss={false}>
Hello
</StyledComponent>,
);

expect(container.firstChild).toMatchInlineSnapshot(`
<div
class="d_flex"
>
Hello
</div>
`);
});

test("css prop usage should win when asChilded onto a styled component", () => {
const StyledOuter = styled(
ark.div,
Expand Down
6 changes: 3 additions & 3 deletions packages/preset-panda/src/plugins/forwardCssPropPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const transformStyledFn = (args: CodegenPrepareHookArgs) => {
factoryJs.code = factoryJs.code.replace(
baseCode,
`${baseCode}
const contextConsume = options.baseComponent ?? Dynamic.__base__ ?? typeof Dynamic === "string"`,
const contextConsume = options.baseComponent || Dynamic.__base__ || typeof Dynamic === "string"`,
);

const propsCode = "const { as: Element = __base__, children, ...restProps } = props";
Expand All @@ -57,7 +57,7 @@ export const transformStyledFn = (args: CodegenPrepareHookArgs) => {

const consume = props.asChild
? consumeCss && (options.baseComponent || Dynamic.__baseComponent__)
: consumeCss ?? contextConsume`,
: consumeCss || contextConsume`,
);

const cvaCode = "const cvaStyles = __cvaFn__.raw(variantProps)";
Expand All @@ -80,7 +80,7 @@ export const transformStyledFn = (args: CodegenPrepareHookArgs) => {
factoryJs.code = factoryJs.code.replace(
styledComponentForwardPropDeclaration,
`${styledComponentForwardPropDeclaration}
StyledComponent.__baseComponent__ = options.baseComponent ?? Dynamic.__baseComponent__`,
StyledComponent.__baseComponent__ = options.baseComponent || Dynamic.__baseComponent__`,
);

const shouldForwardPropCode = "shouldForwardProp?(prop: string, variantKeys: string[]): boolean";
Expand Down
6 changes: 3 additions & 3 deletions packages/styled-system/src/jsx/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ function styledFn(Dynamic, configOrCva = {}, options = {}) {
const __cvaFn__ = composeCvaFn(Dynamic.__cva__, cvaFn)
const __shouldForwardProps__ = composeShouldForwardProps(Dynamic, shouldForwardProp)
const __base__ = Dynamic.__base__ || Dynamic
const contextConsume = options.baseComponent ?? Dynamic.__base__ ?? typeof Dynamic === "string"
const contextConsume = options.baseComponent || Dynamic.__base__ || typeof Dynamic === "string"

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

const consume = props.asChild
? consumeCss && (options.baseComponent || Dynamic.__baseComponent__)
: consumeCss ?? contextConsume
: consumeCss || contextConsume

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

Expand Down Expand Up @@ -65,7 +65,7 @@ function styledFn(Dynamic, configOrCva = {}, options = {}) {
StyledComponent.__cva__ = __cvaFn__
StyledComponent.__base__ = __base__
StyledComponent.__shouldForwardProps__ = shouldForwardProp
StyledComponent.__baseComponent__ = options.baseComponent ?? Dynamic.__baseComponent__
StyledComponent.__baseComponent__ = options.baseComponent || Dynamic.__baseComponent__

return StyledComponent
}
Expand Down
Loading