![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
@solid-primitives/refs
Advanced tools
Library of primitives, components and directives for SolidJS that help managing references to JSX elements.
Collection of primitives, components and directives that help managing references to JSX elements, keeping track of mounted/unmounted elements.
mergeRefs
- Utility for using jsx refs both for local variables and providing it to the props.ref
for component consumers.elements
- Reactive signal that filters out non-element items from a signal array. (Can be used with children
primitive)refs
- Get signal references to Elements of the reactive input. Which were added, which were removed. (Can be used with children
primitive)mapRemoved
- Similar to Solid's mapArray
, but you map the elements that were removed from source array. Leting you keep them for longer.resolveElements
— Will resolve value to a flat list of HTML elements or a single element or null
.unmount
- A directive that calls handler when the element get's unmounted from DOM.<Children>
- Solid's children
helper in component form. Access it's children elements by get
property.<Refs>
- Get up-to-date references of the multiple children elements.<Ref>
- Get up-to-date reference to a single child element.RefProps
- Component properties with types for ref
ResolvedChildren
- Type of resolved JSX elements provided by Solid's children
helper.getChangedItems
- Tells you which elements got added to the array, and which got removedgetAddedItems
- Tells you elements got added to the arraygetRemovedItems
- Tells you which elements got removed from the arraynpm install @solid-primitives/refs
# or
yarn add @solid-primitives/refs
mergeRefs
Utility for using jsx refs both for local variables and providing it to the props.ref
for component consumers.
import { mergeRefs } from "@solid-primitives/refs";
interface ButtonProps {
ref?: HTMLButtonElement | ((el: HTMLButtonElement) => void);
}
const Button = (props: ButtonProps) => {
let ref!: HTMLButtonElement;
onMount(() => {
// use the local ref
});
return <button ref={mergeRefs(el => (ref = el), props.ref)} />;
};
// in consumer's component:
let ref!: HTMLButtonElement;
<Button ref={ref} />;
elements
Reactive signal that filters out non-element items from a signal array. (Can be used with children
primitive)
import { elements } from "@solid-primitives/refs";
const resolved = children(() => props.children);
const els = elements(resolved);
els(); // T: Element[]
// or narrow down the element type
const divs = elements(resolved, HTMLDivElement);
divs(); // T: HTMLDivElement[]
refs
Get signal references to Elements of the reactive input. Which were added, which were removed. (Can be used with children
primitive)
Used internally by <Refs>
component.
import { refs } from "@solid-primitives/refs";
const resolved = children(() => props.children);
const [els, added, removed] = refs(resolved);
els(); // T: Element[]
added(); // T: Element[]
removed(); // T: Element[]
// or narrow down the element type
const [els, added, removed] = refs(resolved, HTMLDivElement);
els(); // T: HTMLDivElement[]
added(); // T: HTMLDivElement[]
removed(); // T: HTMLDivElement[]
mapRemoved
Reactively map removed items from a reactive signal array. If the mapping function return an element signal, this element will be placed in the array returned from primitive.
import { mapRemoved } from "@solid-primitives/refs";
const MyComp = props => {
const resolved = children(() => props.children);
const combined = mapRemoved(resolved, (ref, index) => {
const [el, setEl] = createSignal(ref);
// apply styles/animations to removed element
ref.style.filter = "grayscale(100%)";
// computations can be created inside the mapping fn
createEffect(() => {
// index is a signal
index();
});
const remove = () => {
// ...later
// by setting returned signal to undefined
// element get's removed from combined array permanently
setEl(undefined);
};
// you can return a signal with element to keep it in the combined array
return el;
});
return combined;
};
resolveElements
Similarly to children()
helper from solid-js
will resolve provided value to a flat list of HTML elements or a single element or null
. But doesn't create a computation.
import { resolveElements } from "@solid-primitives/refs";
const MyComponent: ParentComponent = props => {
createEffect(() => {
const resolved = resolveElements(props.children);
resolved; // T: HTMLElement | HTMLElement[] | null
});
return "Don't access props.children here again — it'll create new dom nodes";
};
unmount
A directive that calls handler when the element get's unmounted from DOM.
import { unmount } from "@solid-primitives/refs";
// place it somewhere in the code to prevent it from being tree-shaken
unmount;
const [ref, setRef] = createSignal<Element | undefined>();
<div ref={el => setRef(el)} use:unmount={() => setRef(undefined)}>
Hello
</div>;
<Children>
Solid's children
helper in component form. Access it's children elements by get
property.
import {Children, ResolvedJSXElement} from "@solid-primitives/refs"
// typing as JSX.Element also works
const [children, setChildren] = createSignal<ResolvedJSXElement>([])
<Children get={setChildren}>
<div></div>
...
</Children>
<Ref>
Get up-to-date reference to a single child element.
import { Ref } from "@solid-primitives/refs";
<Ref>
accepts these properties:
ref
- Getter of current element (or undefined
if not mounted)onMount
- handle the child element getting mounted to the domonUnmount
- handle the child element getting unmounted from the domconst [ref, setRef] = createSignal<Element | undefined>();
<Ref
ref={setRef}
onMount={el => console.log("Mounted", el)}
onUnmount={el => console.log("Unmounted", el)}
>
<Show when={show()}>
<div>Hello</div>
</Show>
</Ref>;
<Ref<HTMLDivElement>
ref={el => {...}} // HTMLDivElement | undefined
onMount={el => {...}} // HTMLDivElement
onUnmount={el => {...}} // HTMLDivElement
>
<div>Hello</div>
</Ref>
<Refs>
Get up-to-date references of the multiple children elements.
import { Refs } from "@solid-primitives/refs";
<Refs>
accepts these properties:
refs
- Getter of current array of elementsadded
- Getter of added elements since the last changeremoved
- Getter of removed elements since the last changeonChange
- handle children changesconst [refs, setRefs] = createSignal<Element[]>([]);
<Refs
refs={setRefs}
added={els => console.log("Added elements", els)}
removed={els => console.log("Removed elements", els)}
onChange={e => console.log(e)}
>
<For each={my_list()}>{item => <div>{item}</div>}</For>
<Show when={show()}>
<div>Hello</div>
</Show>
</Refs>;
<Refs<HTMLDivElement>
refs={els => {}} // HTMLDivElement[]
added={els => {}} // HTMLDivElement[]
removed={els => {}} // HTMLDivElement[]
// { refs: HTMLDivElement[]; added: HTMLDivElement[]; removed: HTMLDivElement[] }
onChange={e => {}}
>
<div>Hello</div>
</Refs>
https://stackblitz.com/edit/solid-vite-unocss-bkbgap?file=index.tsx
(run npm start
in the terminal)
RefProps
Component properties with types for ref
interface RefProps<T extends Element> {
ref?: T | ((el: T) => void);
}
ResolvedChildren
Type of resolved JSX elements provided by Solid's children
helper.
type ResolvedChildren = ResolvedJSXElement | ResolvedJSXElement[];
See CHANGELOG.md
FAQs
Library of primitives, components and directives for SolidJS that help managing references to JSX elements.
The npm package @solid-primitives/refs receives a total of 37,870 weekly downloads. As such, @solid-primitives/refs popularity was classified as popular.
We found that @solid-primitives/refs demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.