Skip to main content

ToggleControl

The ToggleControl component lets users select one option from a short list of choices. It can optionally show icons, allow clearing the selection, or stack vertically when space is restricted.

  • When neither value nor defaultValue is set, the first option is selected by default (unless allowEmpty is true).
  • Truncated labels show a tooltip with the full text on hover.
  • Set orientation="auto" to keep equal-width options horizontal when their complete labels fit and stack them vertically when space is restricted. The layout is recalculated after container resizes and font loading.

Import

// with @dhl-official/react-library:
import { DhlToggleControl } from "@dhl-official/react-library"
// with @dhl-official/ui-libraries/react-library:
import { DhlToggleControl } from "@dhl-official/ui-libraries/react-library"

Code​

<DhlToggleControl
options={[
{ label: "Option A", value: "a" },
{ label: "Option B", value: "b" },
{ label: "Option C", value: "c" },
]}
defaultValue="a"
></DhlToggleControl>

Interactive Demo​

Readme​

Properties​

PropertyAttributeDescriptionTypeDefault
allowEmptyallow-emptyAn optional flag that allows users to clear the current selection by reselecting the active option.booleanfalse
dataAriaLabeldata-aria-labelAn optional prop defining the text read by the screen reader to represent the component; use this if you need different text to be read from label.stringundefined
dataClassNamedata-class-nameAn optional class name prop for the component.stringundefined
dataIdidAn optional prop. Gives a valid HTML ID attribute value for the component.string`duil310-dhl-toggle-control-${getRandomString()}`
dataMaskPiidata-mask-piiAn optional prop to mask sensitive data in session-replay tools. When true, sets data-di-mask on the inner element.booleanundefined
dataTestiddata-testidAn optional prop. The test id attached to the component as a data-testid attribute.stringundefined
dataTrackingdata-trackingAn optional data tracking prop for the component.stringundefined
defaultValuedefault-valueAn optional prop defining the initial value when the component is uncontrolled.stringundefined
invertedinvertedAn optional flag to invert the selected item's background color. Only available with Brand Refresh UI enabled.booleanfalse
isDisabledis-disabledAn optional flag to define if the component is disabled.booleanfalse
isFullWidthis-full-width[DEPRECATED] The component now always fills its container. Control width by wrapping in a parent element (e.g. a div with max-width or width). This prop is ignored.

An optional prop that sets the component to 100% width of it's parent container.
booleanfalse
optionsoptionsA REQUIRED array describing the toggle options rendered by the component.DhlToggleControlOption[][]
orientationorientationAn optional orientation for the toggle options. Use auto to render horizontally when every equal-width option fits, otherwise vertically."auto" | "horizontal" | "vertical"DHL_TOGGLE_CONTROL.ORIENTATION.HORIZONTAL
sizesizeAn optional size prop for the toggle options."md" | "sm" | "xs"DHL_TOGGLE_CONTROL.SIZE.MD
valuevalueAn optional prop defining the selected value. If unset, the component can operate in an uncontrolled manner using defaultValue.stringundefined

Events​

EventDescriptionType
dhlBlurEvent emitted when one of the toggle options loses focus.CustomEvent<{ value: string; }>
dhlChangeEvent emitted when the selected option changes.CustomEvent<{ value: string; }>
dhlFocusEvent emitted when one of the toggle options receives focus.CustomEvent<{ value: string; }>

Dependencies​

Depends on​

Graph​


Built by DHL User Interface Library Team!