Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit 2201830

Browse files
committed
feat(stack): wip css stacking
1 parent 3395901 commit 2201830

File tree

7 files changed

+108
-94
lines changed

7 files changed

+108
-94
lines changed

packages/chakra-ui-core/src/CStack/CStack.js

Lines changed: 20 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,16 @@
1-
/**
2-
* Hey! Welcome to @chakra-ui/vue Stack
3-
*
4-
* Stack is a layout utility component that makes
5-
* it easy to stack elements together and apply a space between them.
6-
*
7-
* @see Docs https://vue.chakra-ui.com/stack
8-
* @see Source https://github.com/chakra-ui/chakra-ui-vue/blob/master/packages/chakra-ui-core/src/CStack/CStack.js
9-
*/
10-
11-
import { StringArray, SNA } from '../config/props/props.types'
12-
import { cloneVNode, createStyledAttrsMixin } from '../utils'
1+
import { css } from 'emotion'
132

143
import CFlex from '../CFlex'
15-
import CBox from '../CBox'
4+
import { createStyledAttrsMixin } from '../utils'
165

17-
/**
18-
* CStack component
19-
*
20-
* Flex container to stck it's children
21-
*
22-
* @extends CFlex
23-
* @see Docs https://vue.chakra-ui.com/stack
24-
*/
256
const CStack = {
267
name: 'CStack',
278
mixins: [createStyledAttrsMixin('CStack')],
289
props: {
29-
direction: [String, Array],
30-
isInline: {
31-
type: Boolean,
32-
default: false
33-
},
34-
isReversed: {
35-
type: Boolean,
36-
default: false
37-
},
38-
align: StringArray,
39-
justify: StringArray,
40-
spacing: {
41-
type: SNA,
42-
default: 2
43-
},
44-
shouldWrapChildren: {
45-
type: Boolean,
46-
default: false
47-
}
10+
...CFlex.props,
11+
isInline: [Boolean, String, Array],
12+
isReversed: [Boolean, String, Array],
13+
direction: [Boolean, String, Array]
4814
},
4915
computed: {
5016
_isInline () {
@@ -73,60 +39,28 @@ const CStack = {
7339
}
7440

7541
return _direction
42+
},
43+
spacingProps () {
44+
return this._isInline
45+
? this._isReversed
46+
? { mr: this.spacing, mb: 0 }
47+
: { ml: this.spacing, mt: 0 }
48+
: this._isReversed
49+
? { mb: this.spacing, mr: 0 }
50+
: { mt: this.spacing, ml: 0 }
7651
}
7752
},
7853
render (h) {
79-
const children = this.$slots.default.filter(e => e.tag)
80-
const stackables = children.map((node, index) => {
81-
const isLastChild = children.length === index + 1
82-
const spacingProps = this._isInline
83-
? { [this._isReversed ? 'ml' : 'mr']: isLastChild ? null : this.spacing }
84-
: { [this._isReversed ? 'mt' : 'mb']: isLastChild ? null : this.spacing }
85-
86-
const clone = cloneVNode(node, h)
87-
const { propsData } = clone.componentOptions
88-
const { attrs } = clone.data
89-
90-
// If children nodes should wrap,
91-
// we wrap them inside block with
92-
// display set to inline block.
93-
if (this.shouldWrapChildren) {
94-
return h(CBox, {
95-
props: {
96-
as: this.as,
97-
to: this.to
98-
},
99-
attrs: {
100-
d: 'inline-block',
101-
...spacingProps
102-
}
103-
}, [clone])
104-
}
105-
106-
// Otherwise we simply set spacing props
107-
// to current node.
108-
clone.componentOptions.propsData = {
109-
...propsData
110-
}
111-
112-
clone.data.attrs = {
113-
...attrs,
114-
...spacingProps
115-
}
116-
117-
return clone
118-
})
119-
12054
return h(CFlex, {
121-
class: this.className,
55+
class: [this.className, css({
56+
'& > *:not(template) ~ *:not(template)': this.$chakraSystem(this.spacingProps)
57+
})],
12258
props: {
123-
as: this.as,
124-
align: this.align,
125-
justify: this.justify,
59+
...this.$props,
12660
direction: this._direction
12761
},
12862
attrs: this.computedAttrs
129-
}, stackables)
63+
}, this.$slots.default)
13064
}
13165
}
13266

packages/chakra-ui-core/src/CStack/CStack.stories.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,23 @@ storiesOf('UI | Stack', module)
5353
</div>
5454
`
5555
}))
56+
.add('with HTML elements', () => ({
57+
components: { CHeading, CText, CStack, CBox, CFlex, CIcon },
58+
template: `
59+
<div>
60+
<c-stack spacing="5" align="flex-end" direction="column">
61+
<c-flex align-items="center">
62+
<c-icon mr="3" name="moon" />
63+
<c-text>Wakanda, 🌍 Africa</c-text>
64+
</c-flex>
65+
<p>I am a happy paragraph element</p>
66+
<h3>I am a jolly heading element</h3>
67+
<c-flex align-items="center">
68+
<c-icon mr="3" name="sun" />
69+
<c-text>Kitchener, ON, Canada</c-text>
70+
</c-flex>
71+
<button>O nankana ani?</button>
72+
</c-stack>
73+
</div>
74+
`
75+
}))

packages/chakra-ui-core/src/CStack/tests/CStack.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@ it('should should stack horizontally if isInline', () => {
4747
expect(stack).toHaveStyle('flex-direction: row')
4848
})
4949

50+
it('should should stack native html elements', () => {
51+
const { asFragment } = renderComponent({
52+
template: `
53+
<CStack data-testid="stack">
54+
<CText mt="4">The future can be even brighter but a goal without a plan is just a wish</CText>
55+
<p data-testid="stacked-p">I am a happy paragraph element</p>
56+
<h3 data-testid="stacked-h3">I am a jolly heading element</h3>
57+
<CText mt="4">You deserve good things. With a whooping 10-15% interest rate per annum, grow your savings on your own terms with our completely automated process</CText>
58+
</CStack>
59+
`
60+
})
61+
62+
expect(asFragment()).toMatchSnapshot()
63+
})
64+
5065
// Cannot use `it.each` because it cannot accept
5166
// component as interpolated variable
5267

packages/chakra-ui-core/src/CStack/tests/__snapshots__/CStack.test.js.snap

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
exports[`should render CStack with element as nav 1`] = `
44
<DocumentFragment>
55
<nav
6-
class="css-j7qwjs css-0"
6+
class="css-j7qwjs css-k008qs css-1lp2as9"
77
data-chakra-component="CStack"
88
data-testid="stack"
99
>
1010
<div
11-
class="css-dtyjrz"
11+
class="css-1ipbp6k"
1212
data-chakra-component="CBox"
1313
data-testid="stack-child-1"
1414
>
@@ -26,6 +26,7 @@ exports[`should render CStack with element as nav 1`] = `
2626
The future can be even brighter but a goal without a plan is just a wish
2727
</p>
2828
</div>
29+
2930
<div
3031
class="css-1ipbp6k"
3132
data-chakra-component="CBox"
@@ -52,12 +53,12 @@ exports[`should render CStack with element as nav 1`] = `
5253
exports[`should render CStack with element as section 1`] = `
5354
<DocumentFragment>
5455
<section
55-
class="css-j7qwjs css-0"
56+
class="css-j7qwjs css-k008qs css-1lp2as9"
5657
data-chakra-component="CStack"
5758
data-testid="stack"
5859
>
5960
<div
60-
class="css-dtyjrz"
61+
class="css-1ipbp6k"
6162
data-chakra-component="CBox"
6263
data-testid="stack-child-1"
6364
>
@@ -75,6 +76,7 @@ exports[`should render CStack with element as section 1`] = `
7576
The future can be even brighter but a goal without a plan is just a wish
7677
</p>
7778
</div>
79+
7880
<div
7981
class="css-1ipbp6k"
8082
data-chakra-component="CBox"
@@ -101,12 +103,12 @@ exports[`should render CStack with element as section 1`] = `
101103
exports[`should render correctly 1`] = `
102104
<DocumentFragment>
103105
<div
104-
class="css-j7qwjs css-0"
106+
class="css-j7qwjs css-k008qs css-1lp2as9"
105107
data-chakra-component="CStack"
106108
data-testid="stack"
107109
>
108110
<div
109-
class="css-dtyjrz"
111+
class="css-1ipbp6k"
110112
data-chakra-component="CBox"
111113
data-testid="stack-child-1"
112114
>
@@ -124,6 +126,7 @@ exports[`should render correctly 1`] = `
124126
The future can be even brighter but a goal without a plan is just a wish
125127
</p>
126128
</div>
129+
127130
<div
128131
class="css-1ipbp6k"
129132
data-chakra-component="CBox"
@@ -146,3 +149,39 @@ exports[`should render correctly 1`] = `
146149
</div>
147150
</DocumentFragment>
148151
`;
152+
153+
exports[`should should stack native html elements 1`] = `
154+
<DocumentFragment>
155+
<div
156+
class="css-j7qwjs css-k008qs css-1lp2as9"
157+
data-chakra-component="CStack"
158+
data-testid="stack"
159+
>
160+
<p
161+
class="css-18iq2bq"
162+
data-chakra-component="CText"
163+
>
164+
The future can be even brighter but a goal without a plan is just a wish
165+
</p>
166+
167+
<p
168+
data-testid="stacked-p"
169+
>
170+
I am a happy paragraph element
171+
</p>
172+
173+
<h3
174+
data-testid="stacked-h3"
175+
>
176+
I am a jolly heading element
177+
</h3>
178+
179+
<p
180+
class="css-18iq2bq"
181+
data-chakra-component="CText"
182+
>
183+
You deserve good things. With a whooping 10-15% interest rate per annum, grow your savings on your own terms with our completely automated process
184+
</p>
185+
</div>
186+
</DocumentFragment>
187+
`;

packages/chakra-ui-core/src/CThemeProvider/CThemeProvider.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010

1111
import { colorModeObserver } from '../utils/color-mode-observer'
12+
import { systemProps } from '../utils'
1213

1314
/**
1415
* CThemeProvider component
@@ -24,7 +25,9 @@ const CThemeProvider = {
2425
* If no `ColorModeProvider` is provided in children/ consumer app, all chakra
2526
* components will consume the $chakraColorMode from here.
2627
*/
27-
$chakraColorMode: () => 'light'
28+
$chakraColorMode: () => 'light',
29+
30+
$chakraSystem: props => systemProps({ ...props, theme: this.theme })
2831
}
2932
},
3033
computed: {

packages/chakra-ui-core/src/utils/components.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function createWatcher (property) {
3838
export const createStyledAttrsMixin = (name, isPseudo) => ({
3939
name,
4040
inheritAttrs: false,
41-
inject: ['$chakraTheme', '$chakraColorMode'],
41+
inject: ['$chakraTheme', '$chakraColorMode', '$chakraSystem'],
4242
data () {
4343
return {
4444
attrs$: {},

tests/test-utils/test-utils.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ import userEvent from '@testing-library/user-event'
44

55
import icons from '@/packages/chakra-ui-core/src/lib/internal-icons'
66
import theme from '@/packages/chakra-ui-core/src/lib/theme'
7+
import { systemProps } from '@/packages/chakra-ui-core/src/utils'
8+
79
const defaultProviders = options => ({
810
$chakraTheme: () => theme,
911
$chakraColorMode: () => 'light',
1012
$chakraIcons: icons,
13+
$chakraSystem: props => systemProps({ ...props, theme }),
1114
...options
1215
})
1316

0 commit comments

Comments
 (0)