feat: ssr support for <style vars>

This commit is contained in:
Evan You
2020-07-12 18:04:09 -04:00
parent b6cdd5621e
commit b9595e64cf
17 changed files with 256 additions and 15 deletions

View File

@@ -0,0 +1,27 @@
import { ssrResolveCssVars } from '../src'
describe('ssr: resolveCssVars', () => {
test('should work', () => {
expect(ssrResolveCssVars({ color: 'red' })).toMatchObject({
style: {
'--color': 'red'
}
})
})
test('should work with scopeId', () => {
expect(ssrResolveCssVars({ color: 'red' }, 'scoped')).toMatchObject({
style: {
'--scoped-color': 'red'
}
})
})
test('should strip data-v prefix', () => {
expect(ssrResolveCssVars({ color: 'red' }, 'data-v-123456')).toMatchObject({
style: {
'--123456-color': 'red'
}
})
})
})

View File

@@ -0,0 +1,11 @@
export function ssrResolveCssVars(
source: Record<string, string>,
scopeId?: string
) {
const style: Record<string, string> = {}
const prefix = scopeId ? `${scopeId.replace(/^data-v-/, '')}-` : ``
for (const key in source) {
style[`--${prefix}${key}`] = source[key]
}
return { style }
}

View File

@@ -18,6 +18,7 @@ export {
export { ssrInterpolate } from './helpers/ssrInterpolate'
export { ssrRenderList } from './helpers/ssrRenderList'
export { ssrRenderSuspense } from './helpers/ssrRenderSuspense'
export { ssrResolveCssVars } from './helpers/ssrResolveCssVars'
// v-model helpers
export {