Skip to content

Commit

Permalink
update(MenuToggle): Add prop for custom trigger element. (#304)
Browse files Browse the repository at this point in the history
  • Loading branch information
schillerk committed Mar 30, 2020
1 parent a963e6c commit 9f3ea04
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 22 deletions.
54 changes: 33 additions & 21 deletions packages/core/src/components/MenuToggle/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export type MenuToggleProps = {
toggleIcon?: React.ReactNode;
/** Toggle button text. */
toggleLabel: NonNullable<React.ReactNode>;
/** Custom toggle trigger. */
toggleElement?: React.ReactNode;
/** Z-index of the menu. */
zIndex?: number;
/** @ignore @private */
Expand Down Expand Up @@ -153,6 +155,7 @@ export class MenuToggle extends React.Component<
styles,
toggleIcon,
toggleLabel,
toggleElement,
zIndex,
} = this.props;
let iconSize = '1.5em';
Expand All @@ -165,29 +168,38 @@ export class MenuToggle extends React.Component<

const Button = muted ? MutedButton : BaseButton;

let toggleButton = toggleIcon ? (
<IconButton
disabled={disabled}
aria-label={accessibilityLabel}
onClick={this.handleToggleMenu}
>
{toggleIcon}
</IconButton>
) : (
<Button
disabled={disabled}
afterIcon={<ExpandableIcon expanded={opened} size={iconSize} />}
inverted={inverted}
large={large}
small={small}
onClick={this.handleToggleMenu}
>
{toggleLabel}
</Button>
);

if (toggleElement) {
toggleButton = (
<button type="button" className={cx(styles.customToggle)} onClick={this.handleToggleMenu}>
{toggleElement}
</button>
);
}

return (
<div ref={this.ref} className={cx(styles.container)}>
{toggleIcon ? (
<IconButton
disabled={disabled}
aria-label={accessibilityLabel}
onClick={this.handleToggleMenu}
>
{toggleIcon}
</IconButton>
) : (
<Button
disabled={disabled}
afterIcon={<ExpandableIcon expanded={opened} size={iconSize} />}
inverted={inverted}
large={large}
small={small}
onClick={this.handleToggleMenu}
>
{toggleLabel}
</Button>
)}

{toggleButton}
<div
className={cx(styles.dropdown, !opened && styles.dropdown_hidden, { zIndex })}
aria-expanded={opened}
Expand Down
22 changes: 22 additions & 0 deletions packages/core/src/components/MenuToggle/story.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import IconMenuDots from '@airbnb/lunar-icons/lib/interface/IconMenuDots';
import MenuToggle, { Item } from '.';
import Emoji from '../Emoji';

const children = [
<Item key="0" onClick={() => console.log('onClick')}>
Expand Down Expand Up @@ -35,6 +36,27 @@ aBasicMenuWithMenuItems.story = {
name: 'A basic menu with menu items.',
};

export function withCustomTrigger() {
return (
<MenuToggle
accessibilityLabel="Actions"
toggleElement={
<>
Custom <Emoji unicode="🐱" /> Menu <Emoji unicode="🐱" /> Trigger
</>
}
toggleLabel="Actions"
zIndex={10}
>
{children}
</MenuToggle>
);
}

withCustomTrigger.story = {
name: 'With custom trigger.',
};

export function withCustomIcon() {
return (
<MenuToggle
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/components/MenuToggle/styles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { StyleSheet } from '../../hooks/useStyles';

export const styleSheetMenuToggle: StyleSheet = ({ unit, transition }) => ({
export const styleSheetMenuToggle: StyleSheet = ({ pattern, unit, transition }) => ({
container: {
display: 'inline-block',
position: 'relative',
Expand All @@ -26,4 +26,8 @@ export const styleSheetMenuToggle: StyleSheet = ({ unit, transition }) => ({
display: 'flex',
justifyContent: 'space-between',
},

customToggle: {
...pattern.resetButton,
},
});

0 comments on commit 9f3ea04

Please sign in to comment.