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

Support dynamic value #3146

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/serialize/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ function createStringFromObject(
} else {
for (let key in obj) {
let value = obj[key]
if (typeof value === 'function') {
value = value(mergedProps)
}
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved
if (typeof value !== 'object') {
if (registered != null && registered[value] !== undefined) {
string += `${key}{${registered[value]}}`
Expand Down
33 changes: 19 additions & 14 deletions packages/serialize/types/index.d.ts
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ import * as CSS from 'csstype'
export { RegisteredCache, SerializedStyles }

export type CSSProperties = CSS.PropertiesFallback<number | string>
export type CSSPropertiesWithMultiValues = {
export type CSSPropertiesWithMultiValues<Props> = {
[K in keyof CSSProperties]:
| CSSProperties[K]
| ReadonlyArray<Extract<CSSProperties[K], string>>
| ((props: Props) => number | string)
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved
Andarist marked this conversation as resolved.
Show resolved Hide resolved
}

export type CSSPseudos = { [K in CSS.Pseudos]?: CSSObject }
export type CSSPseudos<Props> = { [K in CSS.Pseudos]?: CSSObject<Props> }

export interface ArrayCSSInterpolation
extends ReadonlyArray<CSSInterpolation> {}
export interface ArrayCSSInterpolation<Props>
extends ReadonlyArray<CSSInterpolation<Props>> {}

export type InterpolationPrimitive =
export type InterpolationPrimitive<Props> =
| null
| undefined
| boolean
Expand All @@ -27,18 +28,22 @@ export type InterpolationPrimitive =
| ComponentSelector
| Keyframes
| SerializedStyles
| CSSObject
| CSSObject<Props>

export type CSSInterpolation = InterpolationPrimitive | ArrayCSSInterpolation
export type CSSInterpolation<Props> =
| InterpolationPrimitive<Props>
| ArrayCSSInterpolation<Props>

export interface CSSOthersObject {
[propertiesName: string]: CSSInterpolation
export interface CSSOthersObject<Props> {
[propertiesName: string]:
| CSSInterpolation<Props>
| ((props: Props) => string | number)
}

export interface CSSObject
extends CSSPropertiesWithMultiValues,
CSSPseudos,
CSSOthersObject {}
export interface CSSObject<Props = unknown>
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved
extends CSSPropertiesWithMultiValues<Props>,
CSSPseudos<Props>,
CSSOthersObject<Props> {}

export interface ComponentSelector {
__emotion_styles: any
Expand All @@ -59,7 +64,7 @@ export interface FunctionInterpolation<Props> {
}

export type Interpolation<Props> =
| InterpolationPrimitive
| InterpolationPrimitive<Props>
| ArrayInterpolation<Props>
| FunctionInterpolation<Props>

Expand Down
11 changes: 11 additions & 0 deletions packages/serialize/types/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ serializeStyles(
{}
)
// $ExpectType SerializedStyles
serializeStyles<{ vars: { background: string; foreground: string } }>([
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved
{
backgroundColor: ({ vars }) => vars.background,
color: ({ vars }) => vars.foreground,
'&:hover': {
backgroundColor: ({ vars }) => vars.foreground,
color: ({ vars }) => vars.background
}
}
])
// $ExpectType SerializedStyles
serializeStyles([testTemplateStringsArray, 5, '4px'], {}, {})

// $ExpectError
Expand Down
12 changes: 12 additions & 0 deletions packages/styled/__tests__/__snapshots__/styled.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,18 @@ exports[`styled objects 1`] = `
</h1>
`;

exports[`styled objects with dynamic value 1`] = `
.emotion-0 {
padding: 0.5rem;
}

<h1
className="emotion-0"
>
hello world
</h1>
`;

exports[`styled objects with spread properties 1`] = `
.emotion-0 {
font-size: 20px;
Expand Down
7 changes: 7 additions & 0 deletions packages/styled/__tests__/styled.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,13 @@ describe('styled', () => {
expect(tree).toMatchSnapshot()
})

test('objects with dynamic value', () => {
const H1 = styled('h1')({ padding: props => props.padding || '1rem' })
const tree = renderer.create(<H1 padding="0.5rem">hello world</H1>).toJSON()

expect(tree).toMatchSnapshot()
})

test('composing components', () => {
const Button = styled.button`
color: green;
Expand Down