|
| 1 | +import { css } from 'emotion' |
| 2 | +import Css from '../Css' |
| 3 | +import styleProps from '../config/props' |
| 4 | +import { camelize, kebabify } from '../utils' |
| 5 | + |
| 6 | +/** Filter attrs and return object of chakra props */ |
| 7 | +function filterChakraProps (attrs) { |
| 8 | + const pure = {} |
| 9 | + for (const prop in attrs) { |
| 10 | + if (styleProps[camelize(prop)]) { |
| 11 | + pure[camelize(prop)] = attrs[prop] |
| 12 | + } |
| 13 | + } |
| 14 | + return pure |
| 15 | +} |
| 16 | + |
| 17 | +/** Purify Chakra attributes */ |
| 18 | +function purifyAttrs (el, props) { |
| 19 | + for (const attr in props) { |
| 20 | + el.removeAttribute(attr) |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +/** Purify's Chakra Attributes from VNode object */ |
| 25 | +function purifyVNodeAttrs (vnode, props) { |
| 26 | + if (props && vnode.data.attrs) { |
| 27 | + for (const attr in props) { |
| 28 | + delete vnode.data.attrs[kebabify(attr)] |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +/** Creates className from styles object */ |
| 34 | +function createClassName (styleObject, theme, hasBindingValue = false) { |
| 35 | + const pure = filterChakraProps(styleObject) |
| 36 | + if (hasBindingValue) { |
| 37 | + const className = css(Css(styleObject)(theme)) |
| 38 | + return [className, pure] |
| 39 | + } |
| 40 | + const className = css(Css(pure)(theme)) |
| 41 | + return [className, pure] |
| 42 | +} |
| 43 | + |
| 44 | +/** Creates SSR `v-chakra` directive for Nuxt */ |
| 45 | +export function createServerDirective (theme) { |
| 46 | + return (vnode, directive) => { |
| 47 | + const [className, pure] = createClassName(vnode.data.attrs, theme) |
| 48 | + if (vnode.data.class) { |
| 49 | + vnode.data.class += ` ${className}` |
| 50 | + } else { |
| 51 | + vnode.data.class = className |
| 52 | + } |
| 53 | + purifyVNodeAttrs(vnode, pure) |
| 54 | + |
| 55 | + if (directive.value) { |
| 56 | + if (typeof directive.value === 'object') { |
| 57 | + const [className, pure] = createClassName(directive.value, theme, true) |
| 58 | + if (vnode.data.class) { |
| 59 | + vnode.data.class += ` ${className}` |
| 60 | + } else { |
| 61 | + vnode.data.class = className |
| 62 | + } |
| 63 | + purifyVNodeAttrs(vnode, pure) |
| 64 | + } |
| 65 | + |
| 66 | + if (typeof directive.value === 'function') { |
| 67 | + const styles = directive.value(theme) |
| 68 | + const [className, pure] = createClassName(styles, theme, true) |
| 69 | + if (vnode.data.class) { |
| 70 | + vnode.data.class += ` ${className}` |
| 71 | + } else { |
| 72 | + vnode.data.class = className |
| 73 | + } |
| 74 | + purifyVNodeAttrs(vnode, pure) |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | +}; |
| 79 | + |
| 80 | +/** Creates Client `v-chakra` Directive */ |
| 81 | +export function createClientDirective (theme) { |
| 82 | + return { |
| 83 | + bind (el, binding, vnode) { |
| 84 | + const [className, pure] = createClassName(vnode.data.attrs, theme) |
| 85 | + el.classList.add(className) |
| 86 | + purifyAttrs(el, pure) |
| 87 | + |
| 88 | + if (binding.value) { |
| 89 | + if (typeof binding.value === 'object') { |
| 90 | + const [className, pure] = createClassName(binding.value, theme, true) |
| 91 | + el.classList.add(className) |
| 92 | + purifyAttrs(el, pure) |
| 93 | + } |
| 94 | + |
| 95 | + if (typeof binding.value === 'function') { |
| 96 | + const styles = binding.value(theme) |
| 97 | + const [className, pure] = createClassName(styles, theme, true) |
| 98 | + el.classList.add(className) |
| 99 | + purifyAttrs(el, pure) |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments