-
- Notifications
You must be signed in to change notification settings - Fork 13
✨Add support for functional elements #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -32,6 +32,10 @@ | |
| * List of children. | ||
| * @typedef {Node | HPrimitiveChild | HArrayChild} HChild | ||
| * Acceptable child value. | ||
| * @typedef {Record<string, unknown>} HComponentProperties | ||
| * Acceptable properties for a component function. | ||
| * @typedef {(props: HComponentProperties) => HResult} HComponent | ||
| * Function returning a hyperscript result (useful for JSX) | ||
| */ | ||
| | ||
| import {find, normalize} from 'property-information' | ||
| | @@ -58,18 +62,38 @@ export function core(schema, defaultTagName, caseSensitive) { | |
| * (selector: null | undefined, ...children: Array<HChild>): Root | ||
| * (selector: string, properties: HProperties, ...children: Array<HChild>): Element | ||
| * (selector: string, ...children: Array<HChild>): Element | ||
| * (component: HComponent, props: HComponentProperties, ...children: Array<HChild>): HResult | ||
| * }} | ||
| */ | ||
| ( | ||
| /** | ||
| * Hyperscript compatible DSL for creating virtual hast trees. | ||
| * | ||
| * @param {string | null | undefined} [selector] | ||
| * @param {HProperties | HChild | null | undefined} [properties] | ||
| * @param {string | null | undefined | HComponent} [selector] | ||
| * @param {HProperties | HChild | null | undefined | HComponentProperties} [properties] | ||
| * @param {Array<HChild>} children | ||
| * @returns {HResult} | ||
| */ | ||
| function (selector, properties, ...children) { | ||
| if (typeof selector === 'function') { | ||
| const props = properties ?? {} | ||
| if (typeof props !== 'object') { | ||
| // We should rarely hit this case because this codepath is only | ||
| // called by JSX transforms, but we have to make the check to | ||
| // satisfy TypeScript. | ||
| throw new TypeError( | ||
| `second argument to h(${ | ||
| selector.name | ||
| }, props) must be an object, but was ${typeof props}` | ||
| ) | ||
| } | ||
| | ||
| return selector({ | ||
| ...props, | ||
| children | ||
| }) | ||
| } | ||
| | ||
| let index = -1 | ||
| /** @type {HResult} */ | ||
| let node | ||
| | @@ -98,6 +122,7 @@ export function core(schema, defaultTagName, caseSensitive) { | |
| } | ||
| } | ||
| } else { | ||
| // @ts-expect-error cannot be `HComponentProperties` because we're not on that codepath. | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn’t it be easier to reuse | ||
| children.unshift(properties) | ||
| } | ||
| } | ||
| | @@ -120,7 +145,7 @@ export function core(schema, defaultTagName, caseSensitive) { | |
| } | ||
| | ||
| /** | ||
| * @param {HProperties | HChild} value | ||
| * @param {HProperties | HChild | HComponentProperties } value | ||
| * @param {string} name | ||
| * @returns {value is HProperties} | ||
| */ | ||
| | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,43 +1,26 @@ | ||||||||||||||||||||||
| import type {HProperties, HChild, HResult} from './core.js' | ||||||||||||||||||||||
| | ||||||||||||||||||||||
| export namespace JSX { | ||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * This defines the return value of JSX syntax. | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| type Element = HResult | ||||||||||||||||||||||
| | ||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * This disallows the use of functional components. | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| type IntrinsicAttributes = never | ||||||||||||||||||||||
| | ||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * This defines the prop types for known elements. | ||||||||||||||||||||||
| * | ||||||||||||||||||||||
| * For `hastscript` this defines any string may be used in combination with `hast` `Properties`. | ||||||||||||||||||||||
| * | ||||||||||||||||||||||
| * This **must** be an interface. | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| // eslint-disable-next-line @typescript-eslint/consistent-indexed-object-style, @typescript-eslint/consistent-type-definitions | ||||||||||||||||||||||
| interface IntrinsicElements { | ||||||||||||||||||||||
| [name: string]: | ||||||||||||||||||||||
| | HProperties | ||||||||||||||||||||||
| | { | ||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * The prop that matches `ElementChildrenAttribute` key defines the type of JSX children, defines the children type. | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| children?: HChild | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| declare global { | ||||||||||||||||||||||
| namespace JSX { | ||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * Return value of JSX syntax. | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| type Element = HResult | ||||||||||||||||||||||
| | ||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * The key of this interface defines as what prop children are passed. | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| // eslint-disable-next-line @typescript-eslint/consistent-type-definitions | ||||||||||||||||||||||
| interface ElementChildrenAttribute { | ||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * Only the key matters, not the value. | ||||||||||||||||||||||
| * This defines the prop types for known elements. | ||||||||||||||||||||||
| * | ||||||||||||||||||||||
| * For `hastscript` this defines any string may be used in combination with `hast` `Properties`. | ||||||||||||||||||||||
| * | ||||||||||||||||||||||
| * This **must** be an interface. | ||||||||||||||||||||||
| Comment on lines +11 to +15 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggested change
| ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| children?: never | ||||||||||||||||||||||
| // eslint-disable-next-line @typescript-eslint/consistent-indexed-object-style, @typescript-eslint/consistent-type-definitions | ||||||||||||||||||||||
| interface IntrinsicElements { | ||||||||||||||||||||||
| [name: string]: | ||||||||||||||||||||||
| | HProperties | ||||||||||||||||||||||
| | { | ||||||||||||||||||||||
| children?: HChild | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| /// <reference types="./jsx-automatic.d.ts"/> | ||
| // Empty (only used for TypeScript). | ||
| export {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we support
h(Component)?h(Component, null)?h(Component, Child)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And
h(Component, {children: ['a']}, 'b')?