Skip to content

Commit

Permalink
Enable css props on styled components (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mad-Kat authored Dec 19, 2024
1 parent 2870ab2 commit 5ce7f16
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .changeset/angry-dingos-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"next-yak": patch
"yak-swc": patch
---

Enable css prop support for styled components, not just native HTML elements by fixing a bug in the types
20 changes: 19 additions & 1 deletion packages/next-yak/runtime/__tests__/cssPropTest.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** @jsxImportSource next-yak */
// this is only a type check file and should not be executed

import { css } from "next-yak";
import { css, styled } from "next-yak";
import { CSSProperties } from "react";

declare module "next-yak" {
Expand Down Expand Up @@ -110,3 +110,21 @@ const ComponentWithInterpolatedCSS = () => {
/>
);
};

const Text = styled.p`
font-size: 20px;
font-weight: bold;
font-family: "Inter";
`;

const StyledComponentWithCSSProp = () => {
<div>
<Text
css={css`
color: red;
`}
>
test
</Text>
</div>;
};
13 changes: 11 additions & 2 deletions packages/next-yak/runtime/styled.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { CSSInterpolation, css, yakComponentSymbol } from "./cssLiteral.js";
import {
CSSInterpolation,
StaticCSSProp,
css,
yakComponentSymbol,
} from "./cssLiteral.js";
import React from "react";

// the following export is not relative as "next-yak/context"
Expand Down Expand Up @@ -84,7 +89,11 @@ type YakComponent<
T,
TAttrsIn extends object = {},
TAttrsOut extends AttrsMerged<T, TAttrsIn> = AttrsMerged<T, TAttrsIn>,
> = FunctionComponent<T> & {
> = FunctionComponent<
T & {
css?: StaticCSSProp;
}
> & {
[yakComponentSymbol]: [
FunctionComponent<T>,
AttrsFunction<T, TAttrsIn, TAttrsOut>,
Expand Down
16 changes: 15 additions & 1 deletion packages/yak-swc/yak_swc/tests/fixture/css-prop/input.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { css } from "next-yak";
import { css, styled } from "next-yak";

const Elem = () => (
<div
Expand Down Expand Up @@ -58,3 +58,17 @@ const Elem6 = () => (
const Elem7 = () => <div className="no-css" />;

const Elem8 = () => <div css={css``} className="empty-css" />;

const Text = styled.p`
font-size: 20px;
`;

const StyledComponentWithCSSProp = () => (
<Text
css={css`
color: red;
`}
>
test
</Text>
);
14 changes: 13 additions & 1 deletion packages/yak-swc/yak_swc/tests/fixture/css-prop/output.dev.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { css, __yak_mergeCssProp } from "next-yak/internal";
import { css, styled, __yak_mergeCssProp } from "next-yak/internal";
import __styleYak from "./input.yak.module.css!=!./input?./input.yak.module.css";
const Elem = ()=><div {.../*YAK Extracted CSS:
.Elem {
Expand Down Expand Up @@ -50,3 +50,15 @@ const Elem7 = ()=><div className="no-css"/>;
const Elem8 = ()=><div {...__yak_mergeCssProp({
className: "empty-css"
}, /*#__PURE__*/ css(__styleYak.Elem8)({}))}/>;
const Text = /*YAK Extracted CSS:
.Text {
font-size: 20px;
}
*/ /*#__PURE__*/ styled.p(__styleYak.Text);
const StyledComponentWithCSSProp = ()=><Text {.../*YAK Extracted CSS:
.StyledComponentWithCSSProp {
color: red;
}
*/ /*#__PURE__*/ css(__styleYak.StyledComponentWithCSSProp)({})}>
test
</Text>;
14 changes: 13 additions & 1 deletion packages/yak-swc/yak_swc/tests/fixture/css-prop/output.prod.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { css, __yak_mergeCssProp } from "next-yak/internal";
import { css, styled, __yak_mergeCssProp } from "next-yak/internal";
import __styleYak from "./input.yak.module.css!=!./input?./input.yak.module.css";
const Elem = ()=><div {.../*YAK Extracted CSS:
.Elem {
Expand Down Expand Up @@ -50,3 +50,15 @@ const Elem7 = ()=><div className="no-css"/>;
const Elem8 = ()=><div {...__yak_mergeCssProp({
className: "empty-css"
}, /*#__PURE__*/ css(__styleYak.Elem8)({}))}/>;
const Text = /*YAK Extracted CSS:
.Text {
font-size: 20px;
}
*/ /*#__PURE__*/ styled.p(__styleYak.Text);
const StyledComponentWithCSSProp = ()=><Text {.../*YAK Extracted CSS:
.StyledComponentWithCSSProp {
color: red;
}
*/ /*#__PURE__*/ css(__styleYak.StyledComponentWithCSSProp)({})}>
test
</Text>;

0 comments on commit 5ce7f16

Please sign in to comment.