-
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!: allow
options
to define refs and props
- Loading branch information
arnoson
committed
Mar 24, 2023
1 parent
c96d9c5
commit 8072694
Showing
12 changed files
with
190 additions
and
92 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
{ | ||
"editor.formatOnSave": true | ||
"editor.formatOnSave": true, | ||
"editor.defaultFormatter": "esbenp.prettier-vscode" | ||
} |
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,13 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Document</title> | ||
</head> | ||
<body> | ||
<div data-simple-component="test" data-message="hello"></div> | ||
<script type="module" src="./src/dev.ts"></script> | ||
</body> | ||
</html> |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { registerComponent, mountComponents } from '.' | ||
|
||
const options = { | ||
props: { message: String, propWithDefault: 123 } | ||
} | ||
|
||
registerComponent('test', options, ({ props }) => { | ||
console.log(props.propWithDefault) | ||
}) | ||
|
||
mountComponents() |
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,4 +1,3 @@ | ||
export { registerComponent } from './registerComponent' | ||
export { registerComponent, defineOptions } from './registerComponent' | ||
export { mountComponent, mountComponents } from './mountComponent' | ||
export { defineProps } from './props' | ||
export * from './types' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,39 @@ | ||
import { SimpleComponent } from './types' | ||
import { | ||
SimpleComponent, | ||
SimpleComponentOptions, | ||
SimpleComponentSetup | ||
} from './types' | ||
|
||
export const components: Record<string, SimpleComponent<any>> = {} | ||
export const getComponent = (el: HTMLElement) => { | ||
const name = el.dataset.simpleComponent | ||
return name ? components[name] : undefined | ||
} | ||
|
||
export const registerComponent = < | ||
T extends HTMLElement, | ||
C extends SimpleComponent<T> | ||
>( | ||
name: string, | ||
component: C | ||
) => { | ||
if (typeof component !== 'function') { | ||
export const defineOptions = (options: SimpleComponentOptions) => options | ||
|
||
export function registerComponent< | ||
Setup extends SimpleComponentSetup<Options>, | ||
Options extends SimpleComponentOptions = {} | ||
>(name: string, setup: Setup): SimpleComponent<any> | ||
|
||
export function registerComponent< | ||
Setup extends SimpleComponentSetup<Options>, | ||
Options extends SimpleComponentOptions = {} | ||
>(name: string, options: Options, setup: Setup): SimpleComponent<any> | ||
|
||
export function registerComponent< | ||
Setup extends SimpleComponentSetup<Options>, | ||
Options extends SimpleComponentOptions = {} | ||
>(name: string, arg1: Options | Setup, arg2?: Setup): SimpleComponent<Options> { | ||
const hasBothArgs = arg2 !== undefined | ||
const options = (hasBothArgs ? arg1 : {}) as Options | ||
const setup = (hasBothArgs ? arg2 : arg1) as Setup | ||
|
||
if (typeof setup !== 'function') | ||
throw new Error(`Component ${name} is not a function.`) | ||
} | ||
|
||
const component = { options, setup } | ||
components[name] = component | ||
return component | ||
} |
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,25 +1,75 @@ | ||
export type SimpleRefs = Record<string, HTMLElement | undefined> | ||
type HTMLElementConstructor = typeof HTMLElement | ||
|
||
export type SimpleRefsAll = Record<string, HTMLElement[]> | ||
type HTMLElementType<T extends HTMLElementConstructor> = T['prototype'] | ||
|
||
export type DefineRefs<T> = SimpleRefs & Partial<T> | ||
type HTMLElementsTypes<T extends Record<string, HTMLElementConstructor>> = { | ||
[K in keyof T]: HTMLElementType<T[K]> | ||
} | ||
|
||
type BuiltInType = Number | String | Array<any> | Object | Boolean | ||
|
||
export type DefineRefsAll<T> = SimpleRefsAll & T | ||
type BuiltInTypeConstructor = | ||
| NumberConstructor | ||
| StringConstructor | ||
| ArrayConstructor | ||
| ObjectConstructor | ||
| BooleanConstructor | ||
|
||
type PropTypes<Definitions extends SimpleComponentOptions['props']> = { | ||
[K in keyof Definitions]: Definitions[K] extends BuiltInTypeConstructor | ||
? ReturnType<Definitions[K]> | undefined | ||
: Definitions[K] extends () => any | ||
? ReturnType<Definitions[K]> | ||
: Definitions[K] | ||
} | ||
|
||
export type SimpleInstance<C extends SimpleComponent> = ReturnType<C> | ||
export type SimpleRefs = Record<string, HTMLElement> | ||
|
||
export type SimpleElement<C extends SimpleComponent, T = HTMLElement> = T & { | ||
$component: SimpleInstance<C> | ||
$refs: SimpleRefs, | ||
$refsAll: SimpleRefsAll | ||
export type SimpleRefsAll<R extends SimpleRefs> = { | ||
[K in keyof R]: R[K][] | ||
} | ||
|
||
export interface SimpleComponentPayload<T> { | ||
el: T | ||
refs: SimpleRefs | ||
refsAll: SimpleRefsAll | ||
export interface SimpleComponentOptions { | ||
el?: HTMLElementConstructor | ||
props?: Record<string, BuiltInType | BuiltInTypeConstructor> | ||
refs?: Record<string, HTMLElementConstructor> | ||
events?: Record<string, any> | ||
} | ||
|
||
export type SimpleComponentPayload< | ||
Options extends SimpleComponentOptions, | ||
Refs extends SimpleRefs = HTMLElementsTypes<NonNullable<Options['refs']>> | ||
> = { | ||
el: Options['el'] extends HTMLElementConstructor | ||
? HTMLElementType<Options['el']> | ||
: HTMLElement | ||
|
||
props: PropTypes<Options['props']> | ||
|
||
refs: Record<string, HTMLElement | undefined> & Partial<Refs> | ||
|
||
refsAll: Record<string, HTMLElement[]> & SimpleRefsAll<Refs> | ||
} | ||
|
||
export type SimpleComponent<T = HTMLElement> = ( | ||
payload: SimpleComponentPayload<T> | ||
export type SimpleComponentSetup<O extends SimpleComponentOptions> = ( | ||
payload: SimpleComponentPayload<O> | ||
) => any | ||
|
||
export type SimpleComponent<Options extends SimpleComponentOptions> = { | ||
setup: SimpleComponentSetup<Options> | ||
options: Options | ||
} | ||
|
||
export type SimpleInstance<Component extends SimpleComponent<any>> = ReturnType< | ||
Component['setup'] | ||
> | ||
|
||
export type SimpleElement< | ||
Component extends SimpleComponent<any>, | ||
Options extends SimpleComponentOptions = Component['options'], | ||
Payload extends SimpleComponentPayload<any> = SimpleComponentPayload<Options> | ||
> = Payload['el'] & { | ||
$component: SimpleInstance<Component> | ||
$refs: Payload['refs'] | ||
$refsAll: Payload['refsAll'] | ||
} |
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,2 @@ | ||
export const isBuiltInTypeConstructor = (value: any) => | ||
[Number, String, Boolean, Array, Object].includes(value) |
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