Skip to content

a trend for composing React UIs

React has revolutionized the way in which we take into accounts UI parts and state
control in UI. However with each new characteristic request or enhancement, a
apparently easy element can temporarily evolve into a fancy amalgamation
of intertwined state and UI common sense.

Consider construction a easy dropdown checklist. First of all, apparently
simple – you set up the open/shut state and design its
look. However, as your software grows and evolves, so do the
necessities for this dropdown:

  • Accessibility Beef up: Making sure your dropdown is usable for
    everybody, together with the ones the use of display screen readers or different assistive
    applied sciences, provides any other layer of complexity. You wish to have to regulate focal point
    states, aria attributes, and make sure your dropdown is semantically
    proper.
  • Keyboard Navigation: Customers shouldn’t be restricted to mouse
    interactions. They could wish to navigate choices the use of arrow keys, choose
    the use of Input, or shut the dropdown the use of Get away. This calls for
    further tournament listeners and state control.
  • Async Knowledge Issues: As your software scales, perhaps the
    dropdown choices don’t seem to be hardcoded anymore. They could be fetched from an
    API. This introduces the wish to set up loading, error, and empty states
    inside the dropdown.
  • UI Diversifications and Theming: Other portions of your software
    would possibly require other types or topics for the dropdown. Managing those
    diversifications inside the element may end up in an explosion of props and
    configurations.
  • Extending Options: Over the years, it’s possible you’ll want further
    options like multi-select, filtering choices, or integration with different
    shape controls. Including those to an already advanced element may also be
    daunting.

Each and every of those issues provides layers of complexity to our dropdown
element. Blending state, common sense, and UI presentation makes it much less
maintainable and bounds its reusability. The extra intertwined they turn into,
the tougher it will get to make adjustments with out unintended unintended effects.

Introducing the Headless Part Development

Dealing with those demanding situations head-on, the Headless Part trend provides
some way out. It emphasizes the separation of the calculation from the UI
illustration, giving builders the ability to construct flexible,
maintainable, and reusable parts.

A Headless Part is a design trend in React the place an element –
usually inplemented as React hooks – is accountable only for common sense and
state control with out prescribing any explicit UI (Person Interface). It
supplies the “brains” of the operation however leaves the “appears to be like” to the
developer enforcing it. In essence, it provides capability with out
forcing a selected visible illustration.

When visualized, the Headless Part seems as a narrow layer
interfacing with JSX perspectives on one facet, and speaking with underlying
records fashions at the different when required. This trend is especially
really helpful for people in search of only the conduct or state control
facet of the UI, because it with ease segregates those from the visible
illustration.

Determine 1: The Headless Part trend

For example, imagine a headless dropdown element. It might take care of
state control for open/shut states, merchandise variety, keyboard
navigation, and so forth. When it is time to render, as a substitute of rendering its personal
hardcoded dropdown UI, it supplies this state and common sense to a kid
serve as or element, letting the developer make a decision the way it must visually
seem.

On this article, we’re going to delve into a realistic instance through developing a
advanced element—a dropdown checklist from the bottom up. As we upload extra
options to the element, we’re going to practice the demanding situations that get up.
Via this, we’re going to display how the Headless Part trend can
deal with those demanding situations, compartmentalize distinct considerations, and assist us
in crafting extra flexible parts.

Imposing a Dropdown Listing

A dropdown checklist is a commonplace element utilized in many puts. Even though
there is a local choose element for elementary use circumstances, a extra complex
model providing extra regulate over every choice supplies a greater consumer
enjoy.

Developing one from scratch, a whole implementation, calls for extra
effort than apparently to start with look. You could imagine
keyboard navigation, accessibility (for example, display screen reader
compatibility), and value on cellular units, amongst others.

We’re going to start with a easy, desktop model that best helps mouse
clicks, and progressively construct in additional options to make it sensible. Be aware
that the function here’s to expose a couple of tool design patterns slightly
than train the best way to construct a dropdown checklist for manufacturing use – in reality, I
don’t counsel doing this from scratch and would as a substitute counsel the use of
extra mature libraries.

Mainly, we’d like a component (let’s name it a cause) for the consumer
to click on, and a state to regulate the display and conceal movements of a listing
panel. First of all, we disguise the panel, and when the cause is clicked, we
display the checklist panel.

import { useState } from "react";

interface Merchandise {
  icon: string;
  textual content: string;
  description: string;
}

sort DropdownProps = {
  pieces: Merchandise[];
};

const Dropdown = ({ pieces }: DropdownProps) => {
  const [isOpen, setIsOpen] = useState(false);
  const [selectedItem, setSelectedItem] = useState<Merchandise | null>(null);

  go back (
    <div className="dropdown">
      <div className="cause" tabIndex={0} onClick={() => setIsOpen(!isOpen)}>
        <span className="variety">
          {selectedItem ? selectedItem.textual content : "Make a selection an merchandise..."}
        </span>
      </div>
      {isOpen && (
        <div className="dropdown-menu">
          {pieces.map((merchandise, index) => (
            <div
              key={index}
              onClick={() => setSelectedItem(merchandise)}
              className="item-container"
            >
              <img src={merchandise.icon} alt={merchandise.textual content} />
              <div className="main points">
                <div>{merchandise.textual content}</div>
                <small>{merchandise.description}</small>
              </div>
            </div>
          ))}
        </div>
      )}
    </div>
  );
};

Within the code above, we have arrange the elemental construction for our dropdown
element. The usage of the useState hook, we set up the isOpen and
selectedItem states to regulate the dropdown’s conduct. A easy click on
at the cause toggles the dropdown menu, whilst deciding on an merchandise
updates the selectedItem state.

Let’s ruin down the element into smaller, manageable items to peer
it extra obviously. This decomposition is not a part of the Headless Part
trend, however breaking a fancy UI element into items is a precious
task.

We will get started through extracting a Cause element to take care of consumer
clicks:

const Cause = ({
  label,
  onClick,
}: {
  label: string;
  onClick: () => void;
}) => {
  go back (
    <div className="cause" tabIndex={0} onClick={onClick}>
      <span className="variety">{label}</span>
    </div>
  );
};

The Cause element is a elementary clickable UI part, taking in a
label to show and an onClick handler. It stays agnostic to its
surrounding context. In a similar way, we will extract a DropdownMenu
element to render the checklist of things:

const DropdownMenu = ({
  pieces,
  onItemClick,
}: {
  pieces: Merchandise[];
  onItemClick: (merchandise: Merchandise) => void;
}) => {
  go back (
    <div className="dropdown-menu">
      {pieces.map((merchandise, index) => (
        <div
          key={index}
          onClick={() => onItemClick(merchandise)}
          className="item-container"
        >
          <img src={merchandise.icon} alt={merchandise.textual content} />
          <div className="main points">
            <div>{merchandise.textual content}</div>
            <small>{merchandise.description}</small>
          </div>
        </div>
      ))}
    </div>
  );
};

The DropdownMenu element shows a listing of things, every with an
icon and an outline. When an merchandise is clicked, it triggers the
equipped onItemClick serve as with the chosen merchandise as its
argument.

After which Inside the Dropdown element, we incorporate Cause
and DropdownMenu and provide them with the important state. This
means guarantees that the Cause and DropdownMenu parts stay
state-agnostic and purely react to handed props.

const Dropdown = ({ pieces }: DropdownProps) => {
  const [isOpen, setIsOpen] = useState(false);
  const [selectedItem, setSelectedItem] = useState<Merchandise | null>(null);

  go back (
    <div className="dropdown">
      <Cause
        label={selectedItem ? selectedItem.textual content : "Make a selection an merchandise..."}
        onClick={() => setIsOpen(!isOpen)}
      />
      {isOpen && <DropdownMenu pieces={pieces} onItemClick={setSelectedItem} />}
    </div>
  );
};

On this up to date code construction, we have separated considerations through developing
specialised parts for various portions of the dropdown, making the
code extra arranged and more straightforward to regulate.

Determine 3: Listing local implementation

As depicted within the symbol above, you’ll be able to click on the “Make a selection an merchandise…”
cause to open the dropdown. Deciding on a worth from the checklist updates
the displayed worth and therefore closes the dropdown menu.

At this level, our refactored code is simple, with every phase
being simple and adaptable. Enhancing or introducing a
other Cause element can be slightly simple.
Then again, as we introduce extra options and set up further states,
will our present parts dangle up?

Let’s to find out with a a the most important enhancement for a significant dopdown
checklist: keyboard navigation.

Imposing Keyboard Navigation

Incorporating keyboard navigation inside our dropdown checklist complements
the consumer enjoy through offering a substitute for mouse interactions.
That is specifically essential for accessibility and gives a continuing
navigation enjoy on the internet web page. Let’s discover how we will reach
this the use of the onKeyDown tournament handler.

First of all, we’re going to connect a handleKeyDown serve as to the onKeyDown
tournament in our Dropdown element. Right here, we make the most of a transfer remark
to resolve the particular key pressed and carry out movements accordingly.
For example, when the “Input” or “Area” secret’s pressed, the dropdown
is toggled. In a similar way, the “ArrowDown” and “ArrowUp” keys permit
navigation during the checklist pieces, biking again to the beginning or finish of
the checklist when important.

const Dropdown = ({ pieces }: DropdownProps) => {
  // ... earlier state variables ...
  const [selectedIndex, setSelectedIndex] = useState<quantity>(-1);

  const handleKeyDown = (e: React.KeyboardEvent) => {
    transfer (e.key) {
      // ... case blocks ...
      // ... dealing with Input, Area, ArrowDown and ArrowUp ...
    }
  };

  go back (
    <div className="dropdown" onKeyDown={handleKeyDown}>
      {/* ... remainder of the JSX ... */}
    </div>
  );
};

Moreover, we’ve got up to date our DropdownMenu element to just accept
a selectedIndex prop. This prop is used to use a highlighted CSS
taste and set the aria-selected characteristic to the these days chosen
merchandise, improving the visible comments and accessibility.

const DropdownMenu = ({
  pieces,
  selectedIndex,
  onItemClick,
}: {
  pieces: Merchandise[];
  selectedIndex: quantity;
  onItemClick: (merchandise: Merchandise) => void;
}) => {
  go back (
    <div className="dropdown-menu" function="listbox">
      {/* ... remainder of the JSX ... */}
    </div>
  );
};

Now, our `Dropdown` element is entangled with each state control code and rendering common sense. It properties an in depth transfer case at the side of the entire state control constructs similar to `selectedItem`, `selectedIndex`, `setSelectedItem`, and so on.

Imposing Headless Part with a Customized Hook

To handle this, we’re going to introduce the concept that of a Headless Part
by means of a customized hook named useDropdown. This hook successfully wraps up
the state and keyboard tournament dealing with common sense, returning an object stuffed
with very important states and purposes. By means of de-structuring this in our
Dropdown element, we stay our code neat and sustainable.

The magic lies within the useDropdown hook, our protagonist—the
Headless Part. This flexible unit properties the entirety a dropdown
wishes: whether or not it is open, the chosen merchandise, the highlighted merchandise,
reactions to the Input key, and so on. The wonder is its
adaptability; you’ll be able to pair it with quite a lot of visible shows—your JSX
components.

const useDropdown = (pieces: Merchandise[]) => {
  // ... state variables ...

  // helper serve as can go back some aria characteristic for UI
  const getAriaAttributes = () => ({
    function: "combobox",
    "aria-expanded": isOpen,
    "aria-activedescendant": selectedItem ? selectedItem.textual content : undefined,
  });

  const handleKeyDown = (e: React.KeyboardEvent) => {
    // ... transfer remark ...
  };
  
  const toggleDropdown = () => setIsOpen((isOpen) => !isOpen);

  go back {
    isOpen,
    toggleDropdown,
    handleKeyDown,
    selectedItem,
    setSelectedItem,
    selectedIndex,
  };
};

Now, our Dropdown element is simplified, shorter and more straightforward to
perceive. It leverages the useDropdown hook to regulate its state and
take care of keyboard interactions, demonstrating a transparent separation of
considerations and making the code more straightforward to grasp and set up.

const Dropdown = ({ pieces }: DropdownProps) => {
  const {
    isOpen,
    selectedItem,
    selectedIndex,
    toggleDropdown,
    handleKeyDown,
    setSelectedItem,
  } = useDropdown(pieces);

  go back (
    <div className="dropdown" onKeyDown={handleKeyDown}>
      <Cause
        onClick={toggleDropdown}
        label={selectedItem ? selectedItem.textual content : "Make a selection an merchandise..."}
      />
      {isOpen && (
        <DropdownMenu
          pieces={pieces}
          onItemClick={setSelectedItem}
          selectedIndex={selectedIndex}
        />
      )}
    </div>
  );
};

Via those changes, we’ve got effectively applied
keyboard navigation in our dropdown checklist, making it extra obtainable and
user-friendly. This case additionally illustrates how hooks can be used
to regulate advanced state and common sense in a structured and modular approach,
paving the way in which for additional improvements and have additions to our UI
parts.

The wonderful thing about this design lies in its distinct separation of common sense
from presentation. By means of ‘common sense’, we discuss with the core functionalities of a
choose element: the open/shut state, the chosen merchandise, the
highlighted part, and the reactions to consumer inputs like urgent the
ArrowDown when opting for from the checklist. This department guarantees that our
element keeps its core conduct with out being certain to a particular
visible illustration, justifying the time period “Headless Part”.

Trying out the Headless Part

The common sense of our element is centralized, enabling its reuse in
various situations. It is the most important for this capability to be dependable.
Thus, complete checking out turns into crucial. The excellent news is,
checking out such conduct is easy.

We will assessment state control through invoking a public way and
watching the corresponding state alternate. For example, we will read about
the connection between toggleDropdown and the isOpen state.

const pieces = [{ text: "Apple" }, { text: "Orange" }, { text: "Banana" }];

it("must take care of dropdown open/shut state", () => {
  const { consequence } = renderHook(() => useDropdown(pieces));

  be expecting(consequence.present.isOpen).toBe(false);

  act(() => {
    consequence.present.toggleDropdown();
  });

  be expecting(consequence.present.isOpen).toBe(true);

  act(() => {
    consequence.present.toggleDropdown();
  });

  be expecting(consequence.present.isOpen).toBe(false);
});

Keyboard navigation exams are fairly extra intricate, essentially due
to the absence of a visible interface. This necessitates a extra
built-in checking out means. One efficient way is crafting a pretend
check element to authenticate the conduct. Such exams serve a twin
function: they supply an educational information on using the Headless
Part and, since they make use of JSX, be offering a real perception into consumer
interactions.

Believe the next check, which replaces the prior state test
with an integration check:

it("cause to toggle", async () => {
  render(<SimpleDropdown />);

  const cause = display screen.getByRole("button");

  be expecting(cause).toBeInTheDocument();

  watch for userEvent.click on(cause);

  const checklist = display screen.getByRole("listbox");
  be expecting(checklist).toBeInTheDocument();

  watch for userEvent.click on(cause);

  be expecting(checklist).now not.toBeInTheDocument();
});

The SimpleDropdown under is a pretend element,
designed completely for checking out. It additionally doubles as a
hands-on instance for customers aiming to put in force the Headless
Part.

const SimpleDropdown = () => {
  const {
    isOpen,
    toggleDropdown,
    selectedIndex,
    selectedItem,
    updateSelectedItem,
    getAriaAttributes,
    dropdownRef,
  } = useDropdown(pieces);

  go back (
    <div
      tabIndex={0}
      ref={dropdownRef}
      {...getAriaAttributes()}
    >
      <button onClick={toggleDropdown}>Make a selection</button>
      <p data-testid="selected-item">{selectedItem?.textual content}</p>
      {isOpen && (
        <ul function="listbox">
          {pieces.map((merchandise, index) => (
            <li
              key={index}
              function="choice"
              aria-selected={index === selectedIndex}
              onClick={() => updateSelectedItem(merchandise)}
            >
              {merchandise.textual content}
            </li>
          ))}
        </ul>
      )}
    </div>
  );
};

The SimpleDropdown is a dummy element crafted for checking out. It
makes use of the centralized common sense of useDropdown to create a dropdown checklist.
When the “Make a selection” button is clicked, the checklist seems or disappears.
This checklist comprises a suite of things (Apple, Orange, Banana), and customers can
choose any merchandise through clicking on it. The exams above make certain that this
conduct works as meant.

With the SimpleDropdown element in position, we are provided to check
a extra intricate but sensible state of affairs.

it("choose merchandise the use of keyboard navigation", async () => {
  render(<SimpleDropdown />);

  const cause = display screen.getByRole("button");

  be expecting(cause).toBeInTheDocument();

  watch for userEvent.click on(cause);

  const dropdown = display screen.getByRole("combobox");
  dropdown.focal point();

  watch for userEvent.sort(dropdown, "{arrowdown}");
  watch for userEvent.sort(dropdown, "{input}");

  watch for be expecting(display screen.getByTestId("selected-item")).toHaveTextContent(
    pieces[0].textual content
  );
});

The check guarantees that customers can choose pieces from the dropdown the use of
keyboard inputs. After rendering the SimpleDropdown and clicking on
its cause button, the dropdown is targeted. Therefore, the check
simulates a keyboard arrow-down press to navigate to the primary merchandise and
an input press to make a choice it. The check then verifies if the chosen merchandise
shows the anticipated textual content.

Whilst using customized hooks for Headless Parts is commonplace, it is not the only real means.
If truth be told, prior to the arrival of hooks, builders hired render props or Upper-Order
Parts to put in force Headless Parts. These days, even if Upper-Order
Parts have misplaced a few of their earlier reputation, a declarative API using
React context is still relatively favoured.

Declarative Headless Part with context API

I’m going to show off another declarative way to reach a identical consequence,
using the React context API on this example. By means of setting up a hierarchy
inside the element tree and making every element replaceable, we will be offering
customers a precious interface that now not best purposes successfully (supporting
keyboard navigation, accessibility, and so forth.), but additionally supplies the versatility
to customise their very own parts.

import { HeadlessDropdown as Dropdown } from "./HeadlessDropdown";

const HeadlessDropdownUsage = ({ pieces }: { pieces: Merchandise[] }) => {
  go back (
    <Dropdown pieces={pieces}>
      <Dropdown.Cause as={Cause}>Make a selection an choice</Dropdown.Cause>
      <Dropdown.Listing as={CustomList}>
        {pieces.map((merchandise, index) => (
          <Dropdown.Possibility
            index={index}
            key={index}
            merchandise={merchandise}
            as={CustomListItem}
          />
        ))}
      </Dropdown.Listing>
    </Dropdown>
  );
};

The HeadlessDropdownUsage element takes an pieces
prop of sort array of Merchandise and returns a Dropdown
element. Inside of Dropdown, it defines a Dropdown.Cause
to render a CustomTrigger element, a Dropdown.Listing
to render a CustomList element, and maps during the
pieces array to create a Dropdown.Possibility for every
merchandise, rendering a CustomListItem element.

This construction permits a versatile, declarative manner of customizing the
rendering and behaviour of the dropdown menu whilst conserving a transparent hierarchical
courting between the parts. Please practice that the parts
Dropdown.Cause, Dropdown.Listing, and
Dropdown.Possibility provide unstyled default HTML components (button, ul,
and li respectively). They every settle for an as prop, enabling customers
to customise parts with their very own types and behaviors.

As an example, we will outline those customised element and use it as above.

const CustomTrigger = ({ onClick, ...props }) => (
  <button className="cause" onClick={onClick} {...props} />
);

const CustomList = ({ ...props }) => (
  <div {...props} className="dropdown-menu" />
);

const CustomListItem = ({ ...props }) => (
  <div {...props} className="item-container" />
);

Determine 4: Declarative Person Interface with customised
components

The implementation is not difficult. We will merely outline a context in
Dropdown (the basis part) and put the entire states wish to be
controlled within, and use that context within the kids nodes so they are able to get right of entry to
the states (or alternate those states by means of APIs within the context).

sort DropdownContextType<T> =  null;
  updateSelectedItem: (merchandise: T) => void;
  getAriaAttributes: () => any;
  dropdownRef: RefObject<HTMLElement>;
;

serve as createDropdownContext<T>()  null>(null);


const DropdownContext = createDropdownContext();

export const useDropdownContext = () => {
  const context = useContext(DropdownContext);
  if (!context) {
    throw new Error("Parts will have to be used inside a <Dropdown/>");
  }
  go back context;
};

The code defines a generic DropdownContextType sort, and a
createDropdownContext serve as to create a context with this sort.
DropdownContext is created the use of this serve as.
useDropdownContext is a customized hook that accesses this context,
throwing an error if it is used out of doors of a <Dropdown/>
element, making sure correct utilization inside the desired element hierarchy.

Then we will outline parts that use the context. We will get started with the
context supplier:

const HeadlessDropdown = <T extends { textual content: string }>({
  kids,
  pieces,
}: {
  kids: React.ReactNode;
  pieces: T[];
}) => {
  const {
    //... the entire states and state setters from the hook
  } = useDropdown(pieces);

  go back (
    <DropdownContext.Supplier
      worth={{
        isOpen,
        toggleDropdown,
        selectedIndex,
        selectedItem,
        updateSelectedItem,
      }}
    >
      <div
        ref={dropdownRef as RefObject<HTMLDivElement>}
        {...getAriaAttributes()}
      >
        {kids}
      </div>
    </DropdownContext.Supplier>
  );
};

The HeadlessDropdown element takes two props:
kids and pieces, and makes use of a customized hook
useDropdown to regulate its state and behaviour. It supplies a context
by means of DropdownContext.Supplier to percentage state and behaviour with its
descendants. Inside a div, it units a ref and applies ARIA
attributes for accessibility, then renders its kids to show
the nested parts, enabling a structured and customizable dropdown
capability.

Be aware how we use useDropdown hook we outlined within the earlier
phase, after which move those values all the way down to the youngsters of
HeadlessDropdown. Following this, we will outline the kid
parts:

HeadlessDropdown.Cause = serve as Cause({
  as: Part = "button",
  ...props
}) {
  const { toggleDropdown } = useDropdownContext();

  go back <Part tabIndex={0} onClick={toggleDropdown} {...props} />;
};

HeadlessDropdown.Listing = serve as Listing({
  as: Part = "ul",
  ...props
}) {
  const { isOpen } = useDropdownContext();

  go back isOpen ? <Part {...props} function="listbox" tabIndex={0} /> : null;
};

HeadlessDropdown.Possibility = serve as Possibility({
  as: Part = "li",
  index,
  merchandise,
  ...props
}) {
  const { updateSelectedItem, selectedIndex } = useDropdownContext();

  go back (
    <Part
      function="choice"
      aria-selected={index === selectedIndex}
      key={index}
      onClick={() => updateSelectedItem(merchandise)}
      {...props}
    >
      {merchandise.textual content}
    </Part>
  );
};

We outlined a kind GenericComponentType to take care of an element or an
HTML tag at the side of any further houses. 3 purposes
HeadlessDropdown.Cause, HeadlessDropdown.Listing, and
HeadlessDropdown.Possibility are outlined to render respective portions of
a dropdown menu. Each and every serve as makes use of the as prop to permit customized
rendering of an element, and spreads further houses onto the rendered
element. All of them get right of entry to shared state and behaviour by means of
useDropdownContext.

  • HeadlessDropdown.Cause renders a button through default that
    toggles the dropdown menu.
  • HeadlessDropdown.Listing renders a listing container if the
    dropdown is open.
  • HeadlessDropdown.Possibility renders person checklist pieces and
    updates the chosen merchandise when clicked.

Those purposes jointly permit a customizable and obtainable dropdown menu
construction.

It in large part boils all the way down to consumer choice on how they make a choice to make use of the
Headless Part of their codebase. In my opinion, I lean in opposition to hooks as they
do not contain any DOM (or digital DOM) interactions; the only real bridge between
the shared state common sense and UI is the ref object. Alternatively, with the
context-based implementation, a default implementation will probably be equipped when the
consumer comes to a decision not to customise it.

Within the upcoming instance, I’m going to display how without difficulty we will
transition to another UI whilst maintaining the core capability with the useDropdown hook.

We are freeing this newsletter in two portions. The second one installment
will display how the headless element may also be tailored to strengthen a brand new
UI, and the way we will lengthen it to fetch its records from a faraway
supply.

We are freeing this newsletter in installments. Long run installments will
describe a type to take into accounts lock-in, and use this type to imagine the
prices of lock-in and the best way to scale back them.

To determine once we put up the following installment subscribe to the
web page’s
RSS feed, Martin’s
Mastodon feed, or his
account on X.


Ready to get a best solution for your business?