series of useful hooks for creating webcomponents
use-current-value
✅
The useCurrentValue
hook allows associating a value with current
based on its parameter, equivalent to:
With the useCurrentValue
hook
Consume the values of a reference without major inconvenience
load global scripts when mounting the component
capture all attributes that are defined in the webcomponent but are not props
Where:
attrs
: all the attributes defined on the webcomponent but that are not props, to facilitate reading this hook transforms the index to camelCase
Share values from the scope via setter and getters
The useClickPress hook will allow you to execute a callback with acceleration according to the click time, for example in the input type number we have 2 buttons by default, an up arrow and a down arrow, these allow us to modify the input value, either:
Increase the value before a click in a unit.
Increase the value by more than one unit according to the click pressure time.
Create reactive templates that interact with the state of your webcomponent
The purpose of this hook is to allow communication between the webcomponent and the lightDOM without knowing the DOM, achieving with this hook:
expose the webcomponent's api to the lightDOM and react to it.
data binding between the state of the webcomponent and the lightDOM.
the useReflectEvent hook reflects the event from the reference origin to the reference destination
the useReflectEvent hook reflects the event from the reference origin to the reference destination, the captured event will be canceled
One of the possibilities of this hook is to reflect the events inside shadowDOM to lightDOM, example when using forms
useRefectEvent will listen for the eventType of refFrom, to reflect it on refTo
Reflects on an element the given event
capture key combinations easily
where:
ref, the reference to associate the event keydown and keyup.
keysCode: key combination to capture
callback: receives the last event of the key combination
The usesClickCoordinates hook is for capturing click coordinates, this is useful when positioning a tooptip or creating visual effects
where:
ref
: node reference to observe the click event.
handlerClick
: Callback that receives the coordinates of the click event.
Where :
x: MouseEvent.clientX
y: MouseEvent.clientY
offset.x : MouseEvent.offsetX
offset.Y : MouseEvent.offsetY
Copies the content in text format of a reference.
Where:
ref
: Node reference to copy
copy
: Callback, executes the content copy event.
creates a bottleneck to the definition of a state, limits concurrency.
this hook is similar to useState, but the purpose of this hook is to bottleneck the state at update time
mode differences
fps:
if delay is set to 1, the update is executed for each cycle of requestAnimationFrame(60fps),
if delay is defined as 2, the update is executed for every 2 cycle of requestAnimationFrame(30fps)
timeout: the delay will be the milliseconds for setTimeout
idle : the delay will be the milliseconds for requestIdleCallback
Communicate the component with external forms.
If the webcomponent is nested within a form tag, this hook will return that form tag as a reference.
If the webcomponent is nested within a form tag, you will be able to listen to events from that tag through useFormListener.
el hook renderiza un input hidden en el lightDOM cuyo name y value del input seran los parametros que el hook resiva
One of the difficult inputs to standardize when working with shadowDOM is the radio input, thanks to this hook you will be able to create and observe a radio input synchronized with the form and its webcomponent.
This hook requires the definition of the properties in its webcomponent:
You can work with the input from the component logic, example:
Your component will automatically be reactive to the change of the states of the radio input
Associate a listener with a reference
Synchronize the state of the disabled prop with the fieldset tag
Inherit the disabled
status of a parent tag type fieldset
, under certain rules:
The label must be on the lightDOM.
The component that uses this hook must declare the prop {disabled: Boolean}
.
Where:
matches
: Optional string, allows to change the search of the parent tag fieldset for another tag or selector compatible with Element.matches.
Inject CSS into the shadowRoot
Inject tag style into shadowRoot with content given as parameter to use CSS.
This hook was not created as a replacement for component.styles, it is rather a utility that seeks to facilitate the integration of css from a customHook, either by defining a state or another action.
create connection between components to share internal states
Now, Atomico includes a context API as part of its core. We recommend implementing it as an alternative to using useChannel
.
An alternative to React's context but solely based on hooks.
Where :
channel
: String
, defines the name of the event to be used to generate the channel.
parentValue
: Value inherited by the parent component.
setChildValue
: Callback
, defines a value for nested components.
This hook is used by @atomico/components/router
The usePromise hook consumes an asynchronous function is ideal for using fetch or other asynchronous tasks.
Where :
result
: Retorno de la promesa
status
: Estado de la promesa:
""
: Without executing.
"pending"
: In action.
"fulfilled"
: Successfully executed.
"rejected"
: Executed with error.
asyncFunction
: asynchronous function.
runFunction
: Booleano
, if true
it will execute the promise and define the status.
optionalArguments
: Optional any[]
, allows to regenerate the promise through arguments.
Declare a state based on a responsive expression similar to using the tag img[srcset].
Where:
state
: String
, Current state according to the comparison between experiment and matchMedia.
expression
: String
, An expression that declares the serialized states.
Where:
defaultState
: Default state this cannot contain the use of commas ", "
.
caseState
: Status to show if matchMedia match.
size: size expression to observe, example:
"1080px": (min-width: 1080px)
"1080x720px": (min-width: 1080px) and (min-height: 720px)
"50rem": (min-width: 50rem)
"50em": (min-width: 50em)
The following example shows the use of useResponsiveState, to display a message based on the mediaquery.
Retrieve a node higher than the current webcomponent.
Where:
selector
: String
, Selector to be used by Element.matches when searching for the parent.
parent
: Element
, ascending search result according to selector.
Where:
parents:
parent nodes of the webcomponent
composed
: bypasses shadow DOM in parent capture.
Observe the size change of a reference.
Where:
ref
: Ref
, reference to observe the resizing.
rect
: Object
, the return of DOMRectReadOnly.toJSON()
, documentation of DOMRect
width
height
x
y
top
right
bottom
left
Where:
ref
: Ref
, reference to observe the resizing.
rect
: Object
, the return of DOMRectReadOnly.toJSON()
, documentation of DOMRect
width
height
x
y
top
right
bottom
left
Retrieves the nodes assigned to a slot.
Where:
ref
: Reference of the slot to observe.
childNodes
: List of nodes assigned to the observed slot.
optionalFilter
: allows to filter nodes assign to childNodes
useProxySlot allows you to observe the nodes assigned to a slot and reassign them to another slot dynamically, example:
Input: Suppose we have a component that observe the slot[name="slide"] node
output: thanks to useProxySlot you will be able to modify the assignment of the list nodes without losing the nodes in the process as normally happens with useSlot, example:
Run a second render before the webcomponent render, ideal for collaborative work between shadowDOM and lightDOM
Observe the changes of a reference using MutationObserver
Observe mutations using a callback
Reflects mutations in a state
share your style sheets between LightDOM and ShadowDOM
This hook creates a style tag in the lightDOM of the component, in order to insert that directly affect the lightDOM, before using this hook consider:
The css is not isolated as if it occurs in the shadowDOM.
It does not support the use of ::slotted selector.
Use a ReactiveController.
Hooks to work with routes in the browser
Hook to handle routes based on expressions according to the https://github.com/uppercod/exp-route library, this hook is used by @atomico/components/router****
Where:
view
: return of the last function executed according to the route match.
path
: string, represents the prop of the last path that consists of the path match.
params
: parameters captured according to the path
search
: parameters captured from the path
Share the return from useRouter
Captures all nodes not created by the webcomponent render, ideal for apply slot polyfill in LightDOM.
Where :
childNodes
: List of nodes that do not belong to the webcomponent render.
update
: Callback that forces a new childNodes
scan.
From the example we can highlight that the webcomponent will use all the children not created by the h1
type and will associate the onclick handler with them.
Allows forcing the rendering of the webcomponent without the need to be tied to a state or property
Since the version of atomico@1.14.* there is the useUpdate hook, with the same functionality but in the core of Atomico.
Where:
forceRender
: Callback
to force rendering of the webcomponent.
Sometimes the rendering of the webcomponent does not depend on a state or property of this, to reflect these changes you can use useForceRender
to regenerate the DOM, example: