8.2.12 • Published 24 days ago

@leafygreen-ui/segmented-control v8.2.12

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
24 days ago

Segmented Control

npm (scoped)

View on MongoDB.design

Installation

Yarn

yarn add @leafygreen-ui/segmented-control

NPM

npm install @leafygreen-ui/segmented-control

Example

import {
  SegmentedControl,
  SegmentedControlOption,
} from '@leafygreen-ui/segmented-control';

<SegmentedControl
  name="fruit"
  size={'default'}
  darkMode={false}
  followFocus={true}
  defaultValue={carrot}
  onChange={(value: string) => {
    console.log(value);
  }}
>
  <SegmentedControlOption value="apple">Apple</SegmentedControlOption>

  <SegmentedControlOption value="banana">Banana</SegmentedControlOption>

  <SegmentedControlOption value="carrot">Carrot</SegmentedControlOption>

  <SegmentedControlOption value="dragonfruit" disabled>
    Dragonfruit
  </SegmentedControlOption>
</SegmentedControl>;

Output HTML

<div>
  <div
    role="tablist"
    aria-label="fruit"
    aria-owns="fruit-0 fruit-1 fruit-2 fruit-3"
  >
    <div data-lg-checked="true">
      <div>
        <div tabindex="-1">
          <button role="tab" id="fruit-0" tabindex="0" aria-selected="true">
            <span>Apple</span>
          </button>
        </div>
        <div data-leafygreen-ui="interaction-ring"></div>
      </div>
    </div>
    <div data-lg-checked="false">
      <div>
        <div tabindex="-1">
          <button role="tab" id="fruit-1" tabindex="-1" aria-selected="false">
            <span>Banana</span>
          </button>
        </div>
        <div data-leafygreen-ui="interaction-ring"></div>
      </div>
    </div>
    <div data-lg-checked="false">
      <div>
        <div tabindex="-1">
          <button role="tab" id="fruit-2" tabindex="-1" aria-selected="false">
            <span>Carrot</span>
          </button>
        </div>
        <div data-leafygreen-ui="interaction-ring"></div>
      </div>
    </div>
    <div data-lg-checked="false">
      <div>
        <div tabindex="-1">
          <button
            role="tab"
            id="fruit-3"
            tabindex="-1"
            aria-selected="false"
            disabled=""
          >
            <span>Dragonfruit</span>
          </button>
        </div>
        <div data-leafygreen-ui="interaction-ring"></div>
      </div>
    </div>
    <div data-leafygreen-ui="selection-indicator"></div>
    <div data-leafygreen-ui="hover-indicator"></div>
  </div>
</div>

Props

PropTypeDescriptionDefault
childrenReact.ReactNodeChildren must be SegmentedControlOptionsnull
sizexsmall / default / largeDefines the size of the segmented control.default
darkModebooleanToggles dark modefalse
defaultValuestringDefines the default, or initial value of the component. Ignored if value is also provided.''
valuestringControls the value of the component. If provided, you must update the value in the onChange method, or other user actions (such as routing)
labelstringA text label to the left of the segmented control. Sets the name prop if none is provided.
namestringIdentifies the segmented control group to screen readers. Auto-generated if no name or label is provided. It's recommended for accessability to set this to a meaningful value for accessibility.
followFocusbooleanDefines whether the selection should automatically follow focus. If set to true, the arrow keys can be used to switch selection, otherwise a keyboard user will need to press enter to make a selection.true
'aria-controls'stringAn id ref that identifies the element(s) whose contents/presence is controlled by the segmented control. Required as a prop on the control, or on each individual option.
classNamestringAdds a className to the outermost element.
onChange(value: string) => voidCallback that gets called when a user makes a new selection.

SegmentedControlOption

Props

PropTypeDescriptionDefault
childrenReact.ReactNodeShould be text.
valuestringRequired. The value of the option.
glyphReact.ReactElementThe icon to display to the left of the option.
disabledbooleanToggles whether the option is disabled.false
asstringRender the option wrapped in another component. Typically used for router Link components.'div'
'aria-controls'stringAn id ref that identifies the element(s) whose contents/presence is controlled by the segmented control. Required as a prop on the control, or on each individual option.
classNamestringAdds a className to the outermost element.
_idstringInternal. A unique identifier for the option${name}-${index}
_checkedbooleanInternal. Identifies whether the option is checked.
_focusedbooleanInternal. Identifies whether the option has focus
_indexnumberInternal. The index of the option
_onClick(value: string) => voidInternal. Calls the onChange callback
_onHover(hovered: boolean) => voidInternal. Fires on mouse in and out
8.2.12

24 days ago

8.2.11

1 month ago

8.2.10

2 months ago

8.2.9

2 months ago

8.2.7

8 months ago

8.2.6

8 months ago

8.2.8

7 months ago

8.2.3

9 months ago

8.2.2

9 months ago

8.2.5

9 months ago

9.0.0-alpha.1

10 months ago

8.2.4

9 months ago

9.0.0-alpha.0

10 months ago

8.2.1

10 months ago

8.2.0

10 months ago

8.1.1-next.2

12 months ago

8.1.1-next.3

12 months ago

8.1.1-next.0

12 months ago

8.1.1-next.1

12 months ago

8.1.1-next.6

12 months ago

8.1.1-next.7

12 months ago

8.1.1-next.4

12 months ago

8.1.1-next.5

12 months ago

8.1.3

11 months ago

8.1.0

12 months ago

8.1.2

11 months ago

8.0.3

12 months ago

8.1.1

11 months ago

8.0.2

12 months ago

7.0.4

1 year ago

7.0.5

1 year ago

8.0.1

1 year ago

8.0.0

1 year ago

7.0.3

1 year ago

6.0.1

1 year ago

6.0.0

1 year ago

7.0.0

1 year ago

7.0.2

1 year ago

7.0.1

1 year ago

3.1.1

2 years ago

3.1.0

2 years ago

5.0.0

2 years ago

4.0.0

2 years ago

3.0.1

2 years ago

3.1.0-test.0

2 years ago

3.1.0-next.1

2 years ago

3.1.0-next.0

2 years ago

3.1.0-next.2

2 years ago

3.0.0

2 years ago

2.0.1

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

2.0.0

2 years ago

1.0.0

2 years ago

0.9.1

2 years ago

0.9.0

3 years ago