-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds class, dataset, and aria attribute jsx prop transformers.
- Loading branch information
Showing
16 changed files
with
140 additions
and
72 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { transformJsxProps } from "./transform-jsx-props" |
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,33 @@ | ||
import { forEach } from "../../../utilities" | ||
|
||
/** | ||
* Given a matcher to a vnode property, apply it to the | ||
* appropriate snabbdom module. | ||
* @param {Object} vnode | ||
* @param {RegExp} matcher | ||
* @param {Function} transformer | ||
*/ | ||
export function setPropToModule(vnode, matcher, transformer) { | ||
const matches = [] | ||
|
||
if (vnode.data) { | ||
for (const key in vnode.data) { | ||
if (matcher.test(key)) | ||
matches.push({ | ||
key, | ||
value: vnode.data[key], | ||
node: vnode, | ||
}) | ||
} | ||
|
||
forEach(matches, transformer) | ||
} | ||
|
||
if (Array.isArray(vnode.children)) { | ||
forEach(vnode.children, (child) => | ||
setPropToModule(child, matcher, transformer) | ||
) | ||
} | ||
|
||
return vnode | ||
} |
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,43 @@ | ||
import { kebabToCamel } from "../../../utilities" | ||
import { setPropToModule } from "./set-prop-to-module" | ||
|
||
/** | ||
* Transform JSX props to snabbdom module data structure. | ||
* @param {Object} vnode | ||
* @returns {Object} vnode | ||
*/ | ||
export function transformJsxProps(vnode) { | ||
setPropToModule(vnode, /^aria-/, ({ key, value, node }) => { | ||
// debugger | ||
if (node.data.attrs) { | ||
node.data.attrs[key] = value | ||
} else { | ||
node.data.attrs = { [key]: value } | ||
} | ||
delete node.data[key] | ||
}) | ||
|
||
setPropToModule(vnode, /^data-/, ({ key, value, node }) => { | ||
const abbrevKey = kebabToCamel(key.slice(5)) | ||
|
||
if (node.data.dataset) { | ||
node.data.dataset[abbrevKey] = value | ||
} else { | ||
node.data.dataset = { [abbrevKey]: value } | ||
} | ||
|
||
delete node.data[key] | ||
}) | ||
|
||
setPropToModule(vnode, /^className$/, ({ value, node }) => { | ||
if (node.data.props) { | ||
node.data.props.className = value | ||
} else { | ||
node.data.props = { className: value } | ||
} | ||
|
||
delete node.data.className | ||
}) | ||
|
||
return vnode | ||
} |
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
export { createUUID } from "./create-uuid" | ||
export { forEach } from "./for-each" | ||
export * from "./is-type" | ||
export { toKebabCase } from "./transform-case" | ||
export { camelToKebab, kebabToCamel } from "./transform-case" | ||
export { sanitizeString } from "./sanitize-string" | ||
export { log } from "./log" |
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
Oops, something went wrong.