Skip to content

Commit c32f714

Browse files
authored
feat: add system.apply utility (#218)
1 parent ae826fa commit c32f714

25 files changed

+128
-33
lines changed

packages/styled-components/src/styled.test.tsx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as React from 'react'
22
import '@testing-library/jest-dom/extend-expect'
33
import { render, cleanup } from '@testing-library/react'
44
import { ThemeProvider, keyframes } from 'styled-components'
5-
import styled, { css } from '.'
5+
import styled, { css, system } from '.'
66

77
afterEach(cleanup)
88

@@ -129,6 +129,26 @@ describe('#styled', () => {
129129
`animation: ${animation.getName()};`,
130130
)
131131
})
132+
133+
it('works with system.apply', () => {
134+
const theme = {
135+
colors: {
136+
primary: 'pink',
137+
},
138+
}
139+
const Dummy = styled.div`
140+
${system.apply({ fontSize: 2, bg: 'primary' })}
141+
`
142+
const { container } = render(
143+
<ThemeProvider theme={theme}>
144+
<Dummy />
145+
</ThemeProvider>,
146+
)
147+
expect(container.firstChild).toHaveStyle(`
148+
font-size: 2px;
149+
background-color: pink;
150+
`)
151+
})
132152
})
133153

134154
describe('#styled.xxx', () => {

packages/system/src/style.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,5 +163,17 @@ describe('#style', () => {
163163
fontFamily: 'arial',
164164
})
165165
})
166+
167+
it('allows apply', () => {
168+
expect(
169+
fontFamily.apply({
170+
fontFamily: 'title',
171+
})({
172+
theme: { fonts: { title: 'arial' } },
173+
}),
174+
).toEqual({
175+
fontFamily: 'arial',
176+
})
177+
})
166178
})
167179
})

packages/system/src/style.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ export function createStyleGenerator(
137137
getStyle: generator,
138138
generators,
139139
}
140+
generator.apply = (values: object) => ({ theme }: IProps) =>
141+
generator({ theme, ...values })
140142
return generator
141143
}
142144

packages/system/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export interface StyleGenerator {
6565
getStyle: StyleGenerator
6666
generators?: StyleGenerator[]
6767
}
68+
apply: (values: object) => (IProps: object) => any
6869
}
6970

7071
export interface TransformValue<TValueType = any> {

website/pages/docs/animations/animation.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ const Button = styled.button`
314314
`
315315
```
316316

317-
To learn more about styled syntax, read [styled syntax documentation](/docs/enhanced-styled-components/).
317+
To learn more about styled syntax, read [styled syntax documentation](/docs/magic-styled-components/).
318318

319319
### Manual
320320

website/pages/docs/core-concepts/adding-base-styles.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function App() {
3737

3838
## Automatic Theming
3939

40-
Of course automatic theming described in [enhanced styled documentation](/docs/enhanced-styled-components/) also applied in `createGlobalStyle`:
40+
Of course automatic theming described in [enhanced styled documentation](/docs/magic-styled-components/) also applied in `createGlobalStyle`:
4141

4242
```js
4343
import { createGlobalStyle } from '@xstyled/...'

website/pages/docs/core-concepts/hover-focus-and-other-states.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
section: Core Concepts
33
title: Hover, Focus, & Other States
44
slug: /docs/hover-focus-other-states/
5-
order: 3
5+
order: 4
66
---
77

88
# Hover, Focus, & Other States

website/pages/docs/core-concepts/enhanced-styled-components.mdx renamed to website/pages/docs/core-concepts/magic-styled-components.mdx

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
---
22
section: Core Concepts
3-
title: Enhanced Styled Components
4-
slug: /docs/enhanced-styled-components/
5-
order: 5
3+
title: Magic Styled Components
4+
slug: /docs/magic-styled-components/
5+
order: 2
66
---
77

8-
# Enhanced Styled Components
8+
# Magic Styled Components
99

10-
How to use xstyled's styled components.
10+
Reduce the boilerplate of your Styled Components.
1111

1212
<carbon-ad />
1313

@@ -26,6 +26,10 @@ const Button = styled.button`
2626
transition: default;
2727
background-color: emerald-500;
2828
color: #fff;
29+
30+
@media (min-width: md) {
31+
font-size: lg;
32+
}
2933
`
3034
```
3135

@@ -35,7 +39,9 @@ Here's how the example above works:
3539
- `semibold` is replaced by `theme.fontWeights.semibold`
3640
- `default` is replaced by `theme.transitions.default`
3741
- `emerald-500` is replaced by `theme.colors['emerald-500']`
38-
- `text` is replaced by `theme.colors['#fff']`, that is not defined, so it uses `#fff` as value
42+
- `#fff` is replaced by `theme.colors['#fff']`, that is not defined, so it uses `#fff` as value
43+
- `@media (min-width: md)` is replaced by `@media (min-width: ${theme.screens.lg})`
44+
- `lg` is replaced by `theme.fontSizes.lg`
3945

4046
## Use another component
4147

@@ -50,6 +56,10 @@ const Button = styled.button`
5056
transition: default;
5157
background-color: emerald-500;
5258
color: #fff;
59+
60+
@media (min-width: md) {
61+
font-size: lg;
62+
}
5363
`
5464

5565
function App() {
@@ -119,9 +129,9 @@ All available theme getters are automatically bound to a theme section and to CS
119129
| `th.transitionProperty` | `transitionProperties` | `transition-property` |
120130
| `th.zIndex` | `zIndices` | `z-index` |
121131

122-
## Create declarative components using styled
132+
## Add utility props to styled components
123133

124-
Sometimes it is convenient to mix styled components and declarative components. It is possible to do it by adding a `Box` suffix to `styled.tag`. Using `styled.buttonBox` instead of `styled.button` let you use any props utilities on the component:
134+
Sometimes it is convenient to mix styled components and utility props. It is possible to do it by adding a `Box` suffix to `styled.tag`. Using `styled.buttonBox` instead of `styled.button` let you use any props utilities on the component:
125135

126136
```js
127137
import styled from '@xstyled/...'
@@ -139,8 +149,58 @@ function App() {
139149
}
140150
```
141151

152+
It is possible to explicitly add utility props by using `system` or any utility as interpolation:
153+
154+
```js
155+
import { system, typography } from '@xstyled/styled-components'
156+
import styled from 'styled-components'
157+
158+
const Button = styled.div`
159+
${system}
160+
`
161+
162+
const Paragraph = styled.p`
163+
${typography}
164+
`
165+
166+
function App() {
167+
return (
168+
<>
169+
<Button bg="red-500">Danger</Button>
170+
<Paragraph fontSize="sm">Hello world!</Paragraph>
171+
</>
172+
)
173+
}
174+
```
175+
176+
## Use utility props to apply style in styled components
177+
178+
Some utilities like `ring` can't be used in styled components. The `apply` method available on each utility or utilities family allows you to apply style directly on your styled components:
179+
180+
```js
181+
import styled, { system } from '@xstyled/...'
182+
183+
const Button = styled.buttonBox`
184+
border-radius: md;
185+
font-weight: semibold;
186+
transition: default;
187+
background-color: emerald-500;
188+
color: #fff;
189+
190+
&:focus {
191+
${system.apply({ ring: 2, ringColor: 'emerald-800' )}
192+
}
193+
`
194+
195+
function App() {
196+
return <Button bg="red-500">Danger</Button>
197+
}
198+
```
199+
142200
## Responsive utilities
143201
202+
**Media queries are automatically transformed by xstyled, these utilities are deprecated and will be removed in next major version. Please use `@media` instead.**
203+
144204
### breakpoints
145205
146206
`breakpoints` lets you write style for each breakpoint.

website/pages/docs/core-concepts/preflight.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
section: Core Concepts
33
title: Preflight
44
slug: /docs/preflight/
5-
order: 2
5+
order: 3
66
---
77

88
# Preflight

website/pages/docs/core-concepts/responsive-design.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
section: Core Concepts
33
title: Responsive Design
44
slug: /docs/responsive-design/
5-
order: 4
5+
order: 5
66
---
77

88
# Responsive Design

0 commit comments

Comments
 (0)