useRefs
Saves all dom elements with a data-ref
attribute to an object. Has some more functionality, documented in its file.
<div data-ref="my-element"></div>
const ref = useRefs()
console.log(ref.myElement) // <div>
Options
root
- Type:
HTMLElement
- Default:
document.body
What element to querySelect in
namespaced
- Type:
Boolean
- Default:
false
Saves only children with [data-ref-namespace]
watch
- Type:
Boolean
- Default:
false
Update refs when the dom changes
asArray
- Type:
Boolean
- Default:
true
Always save elements in arrays
exclude
- Type:
HTMLElement | null
- Default:
null
Element who's chidren wont be saved
Examples
Defaults to finding all elements with a [data-ref]
in <body>
, saving these as camelCased keys on an object:
<body>
<code data-ref="the-element">🐶</code>
</body>
const ref = getRefs()
console.log(ref.theElement) // -> <code>
Multiple [data-ref]
with same value are stored in an array:
<body>
<code data-ref="foo-bar">🐶</code>
<pre data-ref="foo-bar">🐱</pre>
</body>
const ref = getRefs()
console.log(ref.fooBar) // -> [<code>, <pre>]
Pass an element to scope the querySelector:
<body>
<main>
<code data-ref="i-am-found">🐶</code>
</main>
<pre data-ref="discard-me">🐱</pre>
</body>
const ref = getRefs({ root: document.querySelector('main') })
console.log(ref.iAmFound) // -> <code>
console.log(ref.discardMe) // -> undefined
Enable namespace, and only element with the same namespace as root are found:
<body data-ref="my-app">
<code data-ref-my-app="i-am-found">🐶</code>
<pre data-ref="discard-me">🐱</pre>
</body>
const ref = getRefs({ namespace: true })
console.log(ref.iAmFound) // -> <code>
console.log(ref.discardMe) // -> undefined
To save an element under multiple keys, add multiple values separated with a space:
<body>
<code data-ref="first second third">🐶</code>
</body>
const ref = getRefs()
console.log((ref.first === ref.second) === ref.third) // -> true
[data-ref]
with no value are stored under { item }
<body>
<code data-ref>🐶</code>
</body>
const ref = getRefs()
console.log(ref.item) // -> <code>
The root element is stored under { root }
:
<body>
<div data-ref="foo"></div>
</body>
const ref = getRefs()
console.log(ref) // -> { root: <body>, foo: <div> }
If watch is true
, the root will be watched for new elements. Refs are accessed under { current }
, when watch
is enabled.
<body></body>
const ref = getRefs({ watch: true })
document.body.innerHTML = '<button data-ref="new-element">🥸</button>'
// wait for element to be inserted in the DOM
setTimeout(() => {
console.log(ref.current.newElement) // -> <button>
})
Watching can be cancelled by calling unwatch
const ref = getRefs({ watch: true })
ref.unwatch()
document.body.innerHTML = '<button data-ref="new-element">🥸</button>'
setTimeout(() => {
console.log(ref.current.newElement) // -> undefined
})
If the watch argument is a function, this will run on watch-change
const ref = getRefs({ watch: handleChange })
document.body.innerHTML = '<button data-ref="new-element">🥸</button>'
function handleChange() {
console.log(ref.current.newElement) // -> <button>
}
Save all refs as array with asArray:
<body>
<div data-ref="element"></div>
</body>
const ref = getRefs({ asArray: true })
console.log(ref) // -> { element: [<div>] }