-
Notifications
You must be signed in to change notification settings - Fork 372
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add accessible split button component * Refactor Library split button
- Loading branch information
Showing
3 changed files
with
145 additions
and
45 deletions.
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
Composer/packages/lib/ui-shared/src/components/SplitButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
/** @jsx jsx */ | ||
import { jsx } from '@emotion/core'; | ||
import styled from '@emotion/styled'; | ||
import { FluentTheme } from '@fluentui/theme'; | ||
import { DefaultButton, IButtonProps } from '@fluentui/react/lib/Button'; | ||
|
||
interface SplitButtonProps | ||
extends Omit< | ||
IButtonProps, | ||
| 'split' | ||
| 'splitButtonMenuProps' | ||
| 'buttonType' | ||
| 'getSplitButtonClassNames' | ||
| 'renderPersistedMenuHiddenOnMount' | ||
| 'primaryActionButtonProps' | ||
> { | ||
splitButtonAriaLabel: string; | ||
} | ||
|
||
const SplitButtonContainer = styled.div` | ||
position: relative; | ||
display: inline-flex; | ||
label: split-button-container; | ||
`; | ||
|
||
const SplitButtonDelimiter = styled.span( | ||
({ primary, disabled }: { primary?: boolean; disabled?: boolean }) => ` | ||
position: absolute; | ||
width: 1px; | ||
right: 33px; | ||
top: 8px; | ||
bottom: 8px; | ||
background-color: ${ | ||
disabled | ||
? FluentTheme.semanticColors.disabledText | ||
: primary | ||
? FluentTheme.palette.white | ||
: FluentTheme.palette.neutralPrimary | ||
}; | ||
label: split-button-delimiter; | ||
` | ||
); | ||
|
||
const menuButtonStyles = { | ||
root: { | ||
minWidth: '32px', | ||
width: '32px', | ||
marginLeft: '-2px', | ||
}, | ||
label: { | ||
display: 'none', | ||
}, | ||
}; | ||
|
||
/** | ||
* Accessible analog of the Fluent split button component. | ||
* | ||
* Fluent team doesn't have plans to fix a11y issues of the split button in the current version | ||
* as it'd introduce breaking changes. | ||
* For more details see: https://github.com/microsoft/fluentui/issues/21904. | ||
* | ||
* The component intended to mimic Fluent Button API, so it's easier to switch back when accessibility | ||
* concerns are addressed in Fluent. | ||
* | ||
* Unlike Fluent split button this component consists of two buttons. This allows to operate with buttons | ||
* independently and addresses issues with nested button controls not being accessible using screen readers. | ||
*/ | ||
export const SplitButton: React.FC<SplitButtonProps> = ({ | ||
primary, | ||
menuAs, | ||
menuIconProps, | ||
menuProps, | ||
menuTriggerKeyCode, | ||
contextMenu, | ||
onMenuClick, | ||
onAfterMenuDismiss, | ||
onContextMenu, | ||
onRenderMenuIcon, | ||
persistMenu, | ||
onContextMenuCapture, | ||
splitButtonAriaLabel, | ||
disabled, | ||
...props | ||
}) => ( | ||
<SplitButtonContainer> | ||
<DefaultButton disabled={disabled} primary={primary} {...props} /> | ||
<DefaultButton | ||
aria-label={splitButtonAriaLabel} | ||
contextMenu={contextMenu} | ||
disabled={disabled} | ||
menuAs={menuAs} | ||
menuIconProps={menuIconProps} | ||
menuProps={menuProps} | ||
menuTriggerKeyCode={menuTriggerKeyCode} | ||
persistMenu={persistMenu} | ||
primary={primary} | ||
styles={menuButtonStyles} | ||
onAfterMenuDismiss={onAfterMenuDismiss} | ||
onContextMenu={onContextMenu} | ||
onContextMenuCapture={onContextMenuCapture} | ||
onMenuClick={onMenuClick} | ||
onRenderMenuIcon={onRenderMenuIcon} | ||
/> | ||
<SplitButtonDelimiter disabled={disabled} primary={primary} /> | ||
</SplitButtonContainer> | ||
); | ||
|
||
SplitButton.displayName = 'SplitButton'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters