Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to make a dropdown ButtonGroup? #5081

Closed
CleverLemming1337 opened this issue Oct 7, 2024 · 2 comments
Closed

How to make a dropdown ButtonGroup? #5081

CleverLemming1337 opened this issue Oct 7, 2024 · 2 comments
Assignees
Labels

Comments

@CleverLemming1337
Copy link

The website says it's possible to split a button group with a dropdown but it doesn't say how to do in React.

@mperrotti
Copy link
Contributor

@CleverLemming1337 - here's a way you can use ButtonGroup as a split button with a dropdown:

StackBlitz:
Rendered code:

import React from 'react';
import { ButtonGroup, Button, ActionMenu, ActionList } from '@primer/react';
import { TriangleDownIcon } from '@primer/octicons-react';

const actions = ['Action one', 'Action two', 'Action three'];

export default function DropdownSplit() {
  const [selectedActionIndex, setSelectedActionIndex] =
    React.useState<number>(0);
  const selectedAction = actions[selectedActionIndex];

  return (
    <ButtonGroup>
      <Button
        onClick={() => {
          alert(`Activated ${selectedAction}`);
        }}
      >
        {selectedAction}
      </Button>
      <ActionMenu>
        <ActionMenu.Button icon={TriangleDownIcon}>
          More options
        </ActionMenu.Button>
        <ActionMenu.Overlay>
          <ActionList>
            {actions.map((action, index) => {
              return (
                <ActionList.Item
                  key={action}
                  onSelect={() => {
                    setSelectedActionIndex(index);
                  }}
                >
                  {action}
                </ActionList.Item>
              );
            })}
          </ActionList>
        </ActionMenu.Overlay>
      </ActionMenu>
    </ButtonGroup>
  );
}

@CleverLemming1337
Copy link
Author

Oh, this works. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants