-
-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hiccup): update deps & attrib handling
- add @thi.ng/api dep - add support for IDeref attrib vals - add support for `class` attrib obj vals - add `mergeClasses()` helper - add IDeref support inside `class` & `style` attribs - add support for nested data attribs
- Loading branch information
1 parent
7af448f
commit 09ba171
Showing
7 changed files
with
156 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { deref } from "@thi.ng/api"; | ||
import { isString } from "@thi.ng/checks"; | ||
|
||
/** | ||
* Takes a space separated string of existing CSS class names and merges | ||
* it with `val`, which is either another string of class names, an | ||
* object of booleans or an `IDeref` evaluating to either. Returns | ||
* updated class string. | ||
* | ||
* @remarks | ||
* If `val` evaluates to a string, it will be appended to `existing`. | ||
* | ||
* If `val` is an object, its keys are used as class names and their | ||
* values indicate if the class should be added or removed from the | ||
* existing set. | ||
* | ||
* @example | ||
* ```ts | ||
* mergeClasses("foo bar", { foo: false, baz: true }) | ||
* // "bar baz" | ||
* | ||
* mergeClasses("foo bar", "baz"); | ||
* // "baz" | ||
* ``` | ||
* | ||
* @param existing | ||
* @param val | ||
*/ | ||
export const mergeClasses = (existing: string, val: any) => { | ||
val = deref(val); | ||
if (val == null) return existing; | ||
if (isString(val)) return existing + " " + val; | ||
const classes = new Set(existing.split(" ")); | ||
for (let id in val) { | ||
deref(val[id]) ? classes.add(id) : classes.delete(id); | ||
} | ||
return [...classes].join(" "); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters