mirror of
https://github.com/MercuryWorkshop/dreamlandjs.git
synced 2025-05-15 15:10:02 -04:00
change syntax for scoped css
This commit is contained in:
parent
592acdc2e3
commit
d984e35e35
3 changed files with 19 additions and 12 deletions
|
@ -4,4 +4,4 @@ export const DLVERSION = '0.0.9'
|
||||||
export const [USE_MAPFN, TARGET, PROXY, STEPS, LISTENERS, IF, STATEHOOK] =
|
export const [USE_MAPFN, TARGET, PROXY, STEPS, LISTENERS, IF, STATEHOOK] =
|
||||||
Array.from(Array(7), Symbol)
|
Array.from(Array(7), Symbol)
|
||||||
|
|
||||||
export const cssBoundary = 'dl-boundary'
|
export const cssBoundary = 'dlcomponent'
|
||||||
|
|
16
src/core.js
16
src/core.js
|
@ -11,6 +11,10 @@ import {
|
||||||
cssBoundary,
|
cssBoundary,
|
||||||
} from './consts'
|
} from './consts'
|
||||||
|
|
||||||
|
/* FEATURE.CSS.START */
|
||||||
|
import { cssmap, genCss, genuid } from './css'
|
||||||
|
/* FEATURE.CSS.END */
|
||||||
|
|
||||||
// saves a few characters, since document will never change
|
// saves a few characters, since document will never change
|
||||||
let doc = document
|
let doc = document
|
||||||
|
|
||||||
|
@ -309,14 +313,18 @@ export function h(type, props, ...children) {
|
||||||
}
|
}
|
||||||
|
|
||||||
let elm = type.apply(newthis)
|
let elm = type.apply(newthis)
|
||||||
assert(!elm instanceof Array, 'Functional component cannot return a Fragment');
|
assert(
|
||||||
assert(elm instanceof Node, 'Functional component must return a Node');
|
!(elm instanceof Array),
|
||||||
|
'Functional component cannot return a Fragment'
|
||||||
|
)
|
||||||
|
assert(elm instanceof Node, 'Functional component must return a Node')
|
||||||
elm.$ = newthis
|
elm.$ = newthis
|
||||||
newthis.root = elm
|
newthis.root = elm
|
||||||
/* FEATURE.CSS.START */
|
/* FEATURE.CSS.START */
|
||||||
let cl = elm.classList
|
let cl = elm.classList
|
||||||
if (newthis.css) {
|
let css = newthis.css
|
||||||
cl.add(newthis.css)
|
if (css) {
|
||||||
|
cl.add(genCss(`${type.name}-${genuid()}`, css, true))
|
||||||
}
|
}
|
||||||
cl.add(cssBoundary)
|
cl.add(cssBoundary)
|
||||||
/* FEATURE.CSS.END */
|
/* FEATURE.CSS.END */
|
||||||
|
|
13
src/css.js
13
src/css.js
|
@ -1,6 +1,6 @@
|
||||||
import { cssBoundary } from './consts'
|
import { cssBoundary } from './consts'
|
||||||
|
|
||||||
const cssmap = {}
|
export const cssmap = {}
|
||||||
|
|
||||||
/* POLYFILL.SCOPE.START */
|
/* POLYFILL.SCOPE.START */
|
||||||
let scopeSupported
|
let scopeSupported
|
||||||
|
@ -34,7 +34,7 @@ function polyfill_scope(target) {
|
||||||
/* POLYFILL.SCOPE.END */
|
/* POLYFILL.SCOPE.END */
|
||||||
|
|
||||||
export function genuid() {
|
export function genuid() {
|
||||||
return `dl${Array(5)
|
return `${Array(4)
|
||||||
.fill(0)
|
.fill(0)
|
||||||
.map(() => {
|
.map(() => {
|
||||||
return Math.floor(Math.random() * 36).toString(36)
|
return Math.floor(Math.random() * 36).toString(36)
|
||||||
|
@ -49,7 +49,7 @@ const csstag = (scoped) =>
|
||||||
str += f + (values.shift() || '')
|
str += f + (values.shift() || '')
|
||||||
}
|
}
|
||||||
|
|
||||||
return genCss(str, scoped)
|
return genCss(genuid(), str, scoped)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const css = csstag(false)
|
export const css = csstag(false)
|
||||||
|
@ -61,7 +61,7 @@ function parseCombinedCss(str) {
|
||||||
|
|
||||||
// compat layer for older browsers. when css nesting stablizes this can be removed
|
// compat layer for older browsers. when css nesting stablizes this can be removed
|
||||||
str += '\n'
|
str += '\n'
|
||||||
for (;;) {
|
for (; ;) {
|
||||||
let [first, ...rest] = str.split('\n')
|
let [first, ...rest] = str.split('\n')
|
||||||
if (first.trim().endsWith('{')) break
|
if (first.trim().endsWith('{')) break
|
||||||
|
|
||||||
|
@ -73,11 +73,10 @@ function parseCombinedCss(str) {
|
||||||
return [newstr, selfstr, str]
|
return [newstr, selfstr, str]
|
||||||
}
|
}
|
||||||
|
|
||||||
function genCss(str, scoped) {
|
export function genCss(uid, str, scoped) {
|
||||||
let cached = cssmap[str]
|
let cached = cssmap[str]
|
||||||
if (cached) return cached
|
if (cached) return cached
|
||||||
|
|
||||||
const uid = genuid()
|
|
||||||
cssmap[str] = uid
|
cssmap[str] = uid
|
||||||
|
|
||||||
const styleElement = document.createElement('style')
|
const styleElement = document.createElement('style')
|
||||||
|
@ -102,7 +101,7 @@ function genCss(str, scoped) {
|
||||||
}
|
}
|
||||||
/* POLYFILL.SCOPE.END */
|
/* POLYFILL.SCOPE.END */
|
||||||
|
|
||||||
styleElement.textContent = `@scope (.${uid}) to (:not(.${uid}).dl-boundary *) { :scope { ${str} } }`
|
styleElement.textContent = `@scope (.${uid}) to (:not(.${uid}).${cssBoundary} *) { :scope { ${str} } }`
|
||||||
} else {
|
} else {
|
||||||
;[newstr, selfstr, str] = parseCombinedCss(str)
|
;[newstr, selfstr, str] = parseCombinedCss(str)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue