-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
**feat:** mprove the
ElementAttributes
and ReactElementAttributes
…
… JSX type helpers. This greatly improves JSX types for Custom Elements in React JSX, whereas before they only accepted string types for dash-cased attributes. If you have a `get`ter/`set`ter property in your element class, you can now define a dummy property prefixed with `__set__<name-of-the-setter>` to specify the type of the `set`ter, and this will be picked up and lead to improved types in JSX. For example, you can start using like so: Before: ```js @element('some-element') class SomeElement extends Element { @Attribute get someProp(): number {...} @Attribute set someProp(n: number | 'foo' | 'bar') {...} } declare module 'react' { namespace JSX { interface IntrinsicElements { 'some-element': ReactElementAttributes<SomeElement, 'someProp'> } } } ``` and this JSX would have a type error: ```jsx return <some-element some-prop={'foo'} /> // Error: string is not assignable to number ``` After: ```js @element('some-element') class SomeElement extends Element { @Attribute get someProp(): number {...} @Attribute set someProp(n: this['__set__someProp']) {...} /** don't use this property, it is for JSX types. */ __set__someProp!: number | 'foo' | 'bar' } // ... the same React JSX definition as before ... ``` and now JSX prop types will allow setting the *setter* types: ```jsx return <some-element someProp={'foo'} /> // No error, yay! ``` Note, the property is camelCase instead of dash-case now. **BREAKING:** This may introduce type errors into existing JSX templates, tested with React 19 (not tested with React 18 or below), but it is an inevitable upgrade for the better. To migrate, there's likely nothing to do in Solid JSX, but in React JSX the selected properties are no longer converted to dash-case, so you'll want to use the original JS property names in React JSX templates. For example this, ```jsx return <some-element some-prop={...} /> ``` becomes ```jsx return <some-element someProp={...} /> ``` If you have any issues, please reach out on GitHub or Discord! https://discord.gg/VmvkFcWrsx
- Loading branch information
Showing
8 changed files
with
156 additions
and
41 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,13 @@ | ||
import type { HTMLAttributes as ReactHTMLAttributes, DetailedHTMLProps as ReactDetailedHTMLProps } from 'react'; | ||
import type { DashCasedProps } from './utils'; | ||
import type { RemoveAccessors, RemovePrefixes, SetterTypePrefix, WithStringValues } from './LumeElement.js'; | ||
/** | ||
* Similar to ElementAttributes, but for defining element attribute types for | ||
* React JSX. See LUME Element's [TypeScript | ||
* docs](https://docs.lume.io/#/guide/making-elements?id=typescript) for | ||
* details. | ||
*/ | ||
export type ReactElementAttributes<ElementType, SelectedProperties extends keyof ElementType> = ReactDetailedHTMLProps<DashCasedProps<Partial<ToStringValues<Pick<ElementType, SelectedProperties>>>> & ReactHTMLAttributes<ElementType>, ElementType>; | ||
type ToStringValues<Type extends object> = { | ||
[Property in keyof Type]: Type[Property] extends string ? Type[Property] : Type[Property] extends boolean ? boolean | string : string; | ||
}; | ||
export {}; | ||
export type ReactElementAttributes<ElementType extends HTMLElement, SelectedProperties extends keyof RemovePrefixes<RemoveAccessors<ElementType>, SetterTypePrefix>, AdditionalProperties extends object = {}> = Omit<ReactDetailedHTMLProps<ReactHTMLAttributes<ElementType>, ElementType>, SelectedProperties | keyof AdditionalProperties> & { | ||
/** The 'has' attribute from the 'element-behaviors' package. If element-behaviors is installed and imported (it is if you're using `lume` 3D elements) then this specifies which behaviors to instantiate on the given element. */ | ||
has?: string; | ||
} & Partial<WithStringValues<Pick<RemovePrefixes<RemoveAccessors<ElementType>, SetterTypePrefix>, SelectedProperties>>> & AdditionalProperties; | ||
//# sourceMappingURL=react.d.ts.map |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,14 +36,19 @@ | |
}, | ||
"devDependencies": { | ||
"@lume/cli": "^0.14.0", | ||
"@types/react": "^17.0.0", | ||
"@types/react": "npm:types-react@rc", | ||
"@types/react-dom": "npm:types-react-dom@rc", | ||
"ncp": "^2.0.0", | ||
"prettier": "3.0.3", | ||
"typescript": "^5.0.0" | ||
}, | ||
"peerDependencies": { | ||
"@types/react": "*" | ||
}, | ||
"overrides": { | ||
"@types/react": "npm:types-react@rc", | ||
"@types/react-dom": "npm:types-react-dom@rc" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+ssh://[email protected]/lume/element.git" | ||
|
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.