npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@roenlie/lit-fabric

v1.0.54

Published

React hooks syntax for LIT

Downloads

12

Readme

Lit Fabric

npm npm

Fabric offers an alternative syntax for writing web-components through the Lit component and templating system. The syntax and method of component creation is similar to React Hooks. With some minor changes to make it useable with standard web components. While being as minimal of a wrapper as possible.

Example

component('demo-button', () => {
	const [ label, setLabel ] = useProperty('label', 'Click Me!', { type: String });
	const [ counter, setCounter ] = useState('counter', 0, { type: Number });
	const inputQry = useQuery('inputQry', 'input');
	let subCounter = 0;

	useConnected((element) => { });	
	
	useAfterConnected((element) => { });

	useWillUpdate((props, element) => { }, [ 'counter' ]);
	
	useUpdate((props, element) => { }, [ 'counter' ]);

	useUpdated((props, element) => { }, [ 'counter' ]);

	useDisconnected((element) => { });

	useController('testController', {
		hostUpdate() { },
		hostConnected() { },
		hostDisconnected() { },
	});

	useStyles(css`
		button {
			width: 200px;
			aspect-ratio: 1/2;
			border-radius: 12px;
		}
	`);

	return () => html`
	<button @click=${ () => setCounter(counter.value + 1) }>
		${ label?.value } ${ counter.value } ${ subCounter }
	</button>

	<input @input=${ (ev: InputEvent) =>
		setLabel((ev.target as HTMLInputElement).value) } />
	`;
}, {
	base: LitElement,
	mixins: []
}).register();

Api

Fabric uses method overriding as its primary mechanism of creating a valid web-component.
This allows for using all the Lit functionality without excessive amounts of indirection and redirection.

There are hooks available that cover basically all of lits standard component functionality. If a feature is not yet supported, it trivial to create your own hook with the needed implementation.

List of available hooks:

useProperty

useProperty declares a reactive public property on the component.
Equivalent to using the @property() decorator in standard Lit components. Returns a tuple [getter, setter] that can be used to interact with the property. Back to list of hooks.

type UseProperty<T> = (
	name: string,
	initialValue: T,
	options?: PropertyDeclaration<T>
) => readonly [ { value: T; }, (value: T) => void ];


component('demo-button', () => {
	const [ label, setLabel ] = useProperty('label', 'Click Me!');

	return () => html`
	<button>${ label?.value }</button>
	`;
}).register();

useState

Exactly the same as a useProperty, except for it internally using the @state functionality by forcefully setting the state option on the property to true.

This is intended for internal state, as opposed to the useProperty which is intended for values that are set by the consumer of the component. Back to list of hooks.

useQuery

Replicates the behavior of the @query() decorator. Creates a getter that performs a querySelector call for the requested selector. Back to list of hooks.

type UseQuery = <T extends Element = HTMLElement>(
	name: string,
	selector: string,
	cache?: boolean,
) => ({ value: T; });


component('demo-button', () => {
	const buttonQry = useQuery('buttonQry', 'button');

	return () => html`
	<button></button>
	`;
}).register();

useStyles

Acts as the method of applying styles to a component. Internally appends the supplied styles to the components static styles property. Back to list of hooks.

type UseStyles = (css: CSSResultGroup) => void;


component('demo-button', () => {
	useStyles(css`
		button {
			width: 200px;
			aspect-ratio: 1/2;
			border-radius: 12px;
		}
	`);

	return () => html`
	<button></button>
	`;
}).register();

useConnected

Attaches logic to the LitElement connectedCallback method. Back to list of hooks.

type UseConnected = (
	func: (element: LitElement) => void,
) => void;


component('demo-button', () => {
	useConnected((element) => {
		// Called when the element is connected to the DOM.
	});

	return () => html`
	<button></button>
	`;
}).register();

useAfterConnected

Runs the registered function once per connection cycle of the component. Function runs in the updated lifecycle method. Back to list of hooks.

type UseAfterConnected = (
	func: (element: LitElement) => void,
) => void;


component('demo-button', () => {
	useAfterConnected((element) => {
		// Called on the first updated lifecycle method call
		// after each time the component is connected to the DOM.
	});

	return () => html`
	<button></button>
	`;
}).register();

useDisconnected

Attaches logic to the LitElement disconnectedCallback method. Back to list of hooks.

type UseDisconnected = (
	func: (element: LitElement) => void,
) => void;


component('demo-button', () => {
	useDisconnected((element) => {
		// Called when the element is disconnected from the DOM.
	});

	return () => html`
	<button></button>
	`;
}).register();

useWillUpdate

Attaches logic to the LitElement willUpdate method. Back to list of hooks.

type UseWillUpdate = (
	func: (changedProps: PropertyValues, element: LitElement) => void,
	deps?: string[],
) => void;


component('demo-button', () => {
	const [ counter, setCounter ] = useState('counter', 0, { type: Number });

	useWillUpdate((props, element) => {
		// Called during the willUpdate lifecycle method
		// whenever the reactive property: counter is updated.
	}, [ 'counter' ]);

	return () => html`
	<button @click=${() => setCounter(counter.value + 1)}>
		${counter.value}
	</button>
	`;
}).register();

useUpdate

Attaches logic to the LitElement update method. Back to list of hooks.

type UseUpdate = (
	func: (changedProps: PropertyValues, element: LitElement) => void,
	deps?: string[],
) => void;


component('demo-button', () => {
	const [ counter, setCounter ] = useState('counter', 0, { type: Number });

	useUpdate((props, element) => {
		// Called during the update lifecycle method
		// whenever the reactive property: counter is updated.
	}, [ 'counter' ]);

	return () => html`
	<button @click=${() => setCounter(counter.value + 1)}>
		${counter.value}
	</button>
	`;
}).register();

useUpdated

Attaches logic to the LitElement update method. Back to list of hooks.

type UseUpdated = (
	func: (changedProps: PropertyValues, element: LitElement) => void,
	deps?: string[],
) => void;


component('demo-button', () => {
	const [ counter, setCounter ] = useState('counter', 0, { type: Number });

	useUpdate((props, element) => {
		// Called during the updated lifecycle method
		// whenever the reactive property: counter is updated.
	}, [ 'counter' ]);

	return () => html`
	<button @click=${() => setCounter(counter.value + 1)}>
		${counter.value}
	</button>
	`;
}).register();

useController

Attaches a ReactiveController to the component instance. Back to list of hooks.

type UseController = <T extends ReactiveController = ReactiveController>(
	name: string,
	controller: T,
) => ({ value: T; });


component('demo-button', () => {
	useController('testController', {
		hostConnected() {
			// Runs during the components connectedCallback hook.
		},
		hostUpdate() {
			// Runs during the components update hook.
		},
		hostDisconnected() {
			// Runs during the components disconnectedCallback hook.
		},
	});

	return () => html`
	<button></button>
	`;
}).register();

License

MIT