summaryrefslogtreecommitdiff
path: root/src/index.ts
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2023-08-13 14:50:26 +0530
committerAkshay Nair <phenax5@gmail.com>2023-08-13 14:50:26 +0530
commit1f147a779114a641b4abe5e47ac0b05433ec02fb (patch)
treed27845a0ac2deee6bd5ba652da692e733c0cf589 /src/index.ts
parent48455e6caaa7ad98316bbb4896b59f4487ad8650 (diff)
downloadcss-everything-1f147a779114a641b4abe5e47ac0b05433ec02fb.tar.gz
css-everything-1f147a779114a641b4abe5e47ac0b05433ec02fb.zip
feat: finalizes component system. it fucking works holy shit.
Diffstat (limited to 'src/index.ts')
-rw-r--r--src/index.ts35
1 files changed, 27 insertions, 8 deletions
diff --git a/src/index.ts b/src/index.ts
index 1c8184a..80e67d9 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,5 +1,7 @@
import { EvalActions, evalExpr } from './eval'
+import { extractDeclaration, DeclarationEval } from './declarations'
import { parse } from './parser'
+import { match } from './utils/adt'
const UNSET_PROPERTY_VALUE = '<unset>'
const EVENT_HANDLERS = {
@@ -36,9 +38,12 @@ export const getPropertyValue = ($element: Element, prop: string) => {
return !value || value === UNSET_PROPERTY_VALUE ? '' : value
}
-export const getChildrenIds = ($element: Element) => {
+export const getDeclarations = (
+ $element: Element,
+ actions: EvalActions,
+): Promise<Array<DeclarationEval>> => {
const value = getPropertyValue($element, '--cssx-children')
- return value.split(/(\s*,\s*)|\s+/g).filter(Boolean)
+ return extractDeclaration(value, actions)
}
const getEvalActions = ($element: Element, event: any): EvalActions => ({
@@ -132,22 +137,36 @@ export const manageElement = async (
const html = getPropertyValue($element, '--cssx-disgustingly-set-innerhtml')
if (html) $element.innerHTML = html.replace(/(^'|")|('|"$)/g, '')
- const childrenIds = getChildrenIds($element)
- if (childrenIds.length > 0) {
+ const declarations = await getDeclarations(
+ $element,
+ getEvalActions($element, null),
+ )
+ if (declarations.length > 0) {
const LAYER_CLASS = 'cssx-layer'
const $childrenRoot =
$element.querySelector(`:scope > .${LAYER_CLASS}`) ??
Object.assign(document.createElement('div'), { className: LAYER_CLASS })
$element.appendChild($childrenRoot)
- for (const childId of childrenIds) {
- const selector = childId.split('#')
- const [tag, id] = selector.length >= 2 ? selector : ['div', ...selector]
+ for (const declaration of declarations) {
+ const { tag, id, selectors } = declaration.selector
+ const tagName = tag || 'div'
+
let $child = $childrenRoot.querySelector(`:scope > #${id}`)
const isNewElement = !$child
if (!$child) {
- $child = Object.assign(document.createElement(tag || 'div'), { id })
+ $child = Object.assign(document.createElement(tagName), { id })
}
+
+ // Add selectors
+ for (const selector of selectors) {
+ match(selector, {
+ ClassName: cls =>
+ !$child?.classList.contains(cls) && $child?.classList.add(cls),
+ Attr: ([key, val]) => $child?.setAttribute(key, val),
+ })
+ }
+
$childrenRoot.appendChild($child)
await manageElement($child, isNewElement)
}