Etch component

Toggle 1

Toggle 1

Copy Toggle 1

Configurator settings

Change a setting in the configurator to include it.

Optional. Copies the component under its own name, such as News Card Alpha for a site.

Documentation

A button that opens and closes something, such as a mobile menu, by setting data-open on its target. It comes with three animated hamburger icons. MAC Header 1 uses it for its Menu button.

How to use it

  • In Selectors, set Target Element ID, the id of what it opens (without #), and style its open state with [data-open='true']. Toggle ID sets the button's own id, and Next Focus Item ID where focus goes when it opens (self, the button, by default).
  • Label Type adds text: one label, or Alternate, which switches between First Label and Second Label (Open and Close). With the same text in both, Alternate shows it once and doesn't switch. The text is the button's name for screen readers too.
  • Aria Label names a button that has no label ("Toggle Menu"). With a label, clear it, so the label is the name, or start it with the label's text.
  • Name After Label adds words after the label shown, in the name screen readers hear: "menu" makes "Open menu" and "Close menu". It works while Aria Label is empty.
  • Icon Type: Animated Hamburger 1, 2 or 3, a custom SVG (Custom SVG Icon) or none.
  • Target element is a modal? (on by default) marks what it opens as a dialog for screen readers and keeps the keyboard in it. For a disclosure such as a mobile menu, turn it off and turn on Inert Page, which puts the rest of the page out of reach of the keyboard and screen readers while it's open. Freeze Body Scroll stops the page from scrolling while it's open.

Good to know

  • It tells screen readers whether its target is open (aria-expanded), and with a modal target that it opens a dialog (aria-haspopup="dialog"). Escape closes the target, and so does a click outside it or on a link inside it to a section of the same page.
  • Inert Page sets inert on everything but the button and its target, and removes only what it set, so an element the page made inert stays so.
  • The icon's size, line width and color, its gap to the label and the animation's speed are CSS variables, listed in the Reference. The icons are hidden from screen readers.
  • Its tap area is at least --mac-toggle-1-hit-size each way (2.75rem, 44px with a 16px root font size), around a smaller button, without changing the button's box or the row it sits in.
  • Its states are attributes: the target's data-open is "true" while it's open and "false" once closed, and with Freeze Body Scroll the page's body has data-mac-scroll-locked="true" while it's open. The hamburgers' lines are .mac-toggle-1__line, told apart by data-line (top, middle or bottom), and Alternate's two labels are .mac-toggle-1__label-text, by data-label (off or on); with the same text in both, its one label has no data-label.
  • In MAC Header 1 it shows "Menu", with Inert Page and Freeze Body Scroll on and the menu not a modal.

Reference

Read from the component's JSON, so it's always current.

Loading the reference