Skip to content

Releases: atomicojs/hooks

add the useHistory hook

21 Mar 03:44

Choose a tag to compare

useHistory

stores the first parameter in a list, to keep a history of the values between renders

Example

import { useHistory } from "@atomico/hooks/use-history";

function component({ value }){
    const history = useHistory( value );

    return <host>
        current: {history.at(-1)}
        previous: {history.at(-2)}
    </host>
}

Now the parameter for useCssLightDom is of type Sheets

06 Mar 20:57

Choose a tag to compare

This aligns the behavior of Atomico style sheets from shadowDOM to LightDOM

Example

const sheets = [
    css`:host{color: black}`,
    css`:host{color: orange}`,
];

function component(){
    useCssLightDom(sheets);

    return <host></host>
}

add the useMediaQuery hook

22 Feb 02:36

Choose a tag to compare

useMediaQuery

Listen and set the state of a media query expression.

Example

import { useMediaQuery } from "@atomico/hooks/use-media-query";

function component(){
    const small = useMediaQuery("(max-width: 520px)");

    return <host>{small? "view small": "view large"}</host>;
}

correctly synchronizes the lists associated to the dynamic slots

17 Feb 16:39

Choose a tag to compare

3.30.2

correctly synchronizes the lists associated to the dynamic slots

Fix var useHost

17 Feb 16:10

Choose a tag to compare

3.30.1

fix var

add useProxySlot

17 Feb 15:54

Choose a tag to compare

useProxySlot

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

<my-component>
    <img slot="slide" src="slide-1"/>
    <img slot="slide" src="slide-1"/>
    <img slot="slide" src="slide-1"/>
</my-component>

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:

<my-component>
    <img slot="slide-1" src="slide-1"/>
    <img slot="slide-2" src="slide-1"/>
    <img slot="slide-3" src="slide-1"/>
</my-component>

Syntax and example

import { useRef } from "atomico";
import { useProxySlot } from "@atomico/hooks/use-slot";

function component() {
  const ref = useRef();
  const children = useProxySlot(ref);

  return (
    <host shadowDom>
      <slot name="slide" ref={ref} />
      {children.map((child, index) => (
        <slot name={(child.slot = "slide-" + index)} />
      ))}
    </host>
  );
}

add the useClickPress hook

06 Feb 04:39

Choose a tag to compare

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:

  1. Increase the value before a click in a unit.
  2. Increase the value by more than one unit according to the click pressure time.

Example

import { useClickPress } from "@atomico/hooks/use-click-press";

function counter() {
  const refButton = useRef();

  const [value, setValue] = useProp("value");

  const increment = () => setValue((value) => value + 1);

  useClickPress(refButton, increment);

  return (
    <host>
      <h1>value: {value}</h1>
      <button ref={refButton}>Increment</button>
    </host>
  );
}

counter.props = { value: { type: Number, value: 0 } };

Api change in hook use-css-light-dom

04 Feb 16:59

Choose a tag to compare

The purpose of this update is:

  1. Simplify CSS injection in the lightDOM.
  2. Be consistent in syntax with that provided in ShadowDOM.
  3. Reduce the dependencies to inject the CSS in the lightDOM.
import { css } from "atomico";
import { useCssLightDom } from "@atomico/hooks/use-css-light-dom";

const sheet = css`
  :host {
    width: 200px;
    height: 200px;
    background: black;
  }
`;

function component() {
  useCssLightDom(sheet);
  return <host></host>;
}

Prefer the use of css in shadowDOM, as this hook does not achieve absolute scope.

Add new hook useUniqueIdSelector

04 Feb 13:46

Choose a tag to compare

the useUniqueIdSelector hook creates a unique id on mount, ideal for creating selectors exposed in the lightDOM, example:

import { useUniqueIdSelector } from "@atomico/hooks/use-unique-id-selector";

function component() {
  const selectorId = useUniqueIdSelector();

  return (
    <host>
      <input id={selectorId} />
      <style>{`${selectorId}{ background: black }`}</style>
    </host>
  );
}

fix useReflectEvent

21 Jan 03:38

Choose a tag to compare

The bug was the emission of 2 events if the destination target was heard, the way of capturing is corrected and thus avoiding a double emission of the event in the destination target.