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

Commit 6640f20

Browse files
committed
feat(v-chakra): support callback as directive value
1 parent 6925680 commit 6640f20

File tree

3 files changed

+67
-19
lines changed

3 files changed

+67
-19
lines changed
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
import CBox from './CBox'
22
export default CBox
3-
export * from './CBox'

packages/chakra-ui-core/src/directives/chakra.directive.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { css } from 'emotion'
2-
import __css from '@styled-system/css'
3-
import { systemProps } from '../CBox'
2+
import Css from '../Css'
43
import styleProps from '../config/props'
5-
import { forwardProps, camelize } from '../utils'
6-
import { parsePseudoStyles } from '../CPseudoBox/utils'
4+
import { camelize } from '../utils'
75

86
/** Filter attrs and return object of chakra props */
97
function filterChakraProps (attrs) {
@@ -24,27 +22,38 @@ function purifyAttrs (el, props) {
2422
}
2523

2624
/** Creates className from styles object */
27-
function createClassName (styleObject, theme) {
28-
const pure = filterChakraProps(forwardProps(styleObject))
29-
const { pseudoStyles, baseProps } = parsePseudoStyles(pure)
30-
const baseStyles = systemProps({ ...baseProps, theme })
31-
const _pseudoStyles = __css(pseudoStyles)(theme)
32-
const className = css({ ...baseStyles, ..._pseudoStyles })
25+
function createClassName (styleObject, theme, hasBindingValue = false) {
26+
const pure = filterChakraProps(styleObject)
27+
if (hasBindingValue) {
28+
const className = css(Css(styleObject)(theme))
29+
return [className, pure]
30+
}
31+
const className = css(Css(pure)(theme))
3332
return [className, pure]
3433
}
3534

3635
/** Creates Chakra Directive */
3736
export default function createCharkaDirective (theme) {
3837
return {
3938
bind (el, binding, vnode) {
39+
console.log(vnode)
4040
const [className, pure] = createClassName(vnode.data.attrs, theme)
4141
el.classList.add(className)
4242
purifyAttrs(el, pure)
4343

44-
if (binding.value && typeof binding.value === 'object') {
45-
const [className, pure] = createClassName(binding.value, theme)
46-
el.classList.add(className)
47-
purifyAttrs(el, pure)
44+
if (binding.value) {
45+
if (typeof binding.value === 'object') {
46+
const [className, pure] = createClassName(binding.value, theme, true)
47+
el.classList.add(className)
48+
purifyAttrs(el, pure)
49+
}
50+
51+
if (typeof binding.value === 'function') {
52+
const styles = binding.value(theme)
53+
const [className, pure] = createClassName(styles, theme, true)
54+
el.classList.add(className)
55+
purifyAttrs(el, pure)
56+
}
4857
}
4958
}
5059
}

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

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,54 @@ import { storiesOf } from '@storybook/vue'
22

33
storiesOf('Directives | Chakra', module)
44
.add('Base usage - No arguments', () => ({
5+
template: `
6+
<div>
7+
<div v-chakra p="3" bg="red.100" rounded="md" color="red.500" font-weight="bold">Welcome to Chakra directive</div>
8+
</div>
9+
`
10+
}))
11+
.add('With value | Styles object', () => ({
512
template: `
613
<div>
714
<div v-chakra="{
8-
_hover: {
9-
color: 'green.400',
10-
fontWeight: 'bold'
15+
p: 3,
16+
shadow: 'sm',
17+
h1: {
18+
bg: 'blue.100'
19+
},
20+
}">
21+
<h1>Title</h1>
22+
<p>Text</p>
23+
</div>
24+
</div>
25+
`
26+
}))
27+
.add('With value | Function', () => ({
28+
template: `
29+
<div>
30+
<div v-chakra="theme => ({
31+
shadow: 'sm',
32+
bg: theme.colors.blue[800],
33+
color: theme.colors.yellow[300],
34+
p: {
35+
fontWeight: 'bold',
36+
p: 3
1137
}
12-
}" p="3" color="red.400" font-weight="bold">Welcome to Chakra directive</div>
38+
})">
39+
<p>Computed styles</p>
40+
</div>
41+
</div>
42+
`
43+
}))
44+
.add('Demo button', () => ({
45+
template: `
46+
<div>
47+
<button v-chakra="{
48+
':hover': { bg: 'green.400' },
49+
':focus': { shadow: 'outline' }
50+
}" font-weight="bold" px="4" py="3" color="white" rounded="md" bg="blue.400" outline="none">
51+
Button
52+
</button>
1353
</div>
1454
`
1555
}))

0 commit comments

Comments
 (0)