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

allow for centered dropdown menus #449

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 5 additions & 14 deletions src/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ const propTypes = {
itemSelector: PropTypes.string.isRequired,

/**
* Align the menu to the 'end' side of the placement side of the Dropdown toggle. The default placement is `top-start` or `bottom-start`.
* Align the menu to the 'center' or 'end' side of the placement side of the Dropdown toggle. The default placement is `top-start` or `bottom-start`.
*/
alignEnd: PropTypes.bool,
align: PropTypes.string,

/**
* Whether or not the Dropdown is visible.
Expand Down Expand Up @@ -97,7 +97,7 @@ const defaultProps = {
*/
function Dropdown({
drop,
alignEnd,
align,
defaultShow,
show: rawShow,
onToggle: rawOnToggle,
Expand Down Expand Up @@ -144,22 +144,13 @@ function Dropdown({
toggle,
drop,
show,
alignEnd,
align,
menuElement,
toggleElement,
setMenu,
setToggle,
}),
[
toggle,
drop,
show,
alignEnd,
menuElement,
toggleElement,
setMenu,
setToggle,
],
[toggle, drop, show, align, menuElement, toggleElement, setMenu, setToggle],
);

if (menuElement && lastShow && !show) {
Expand Down
2 changes: 1 addition & 1 deletion src/DropdownContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const DropdownContext = React.createContext({
toggleRef() {},
onToggle() {},
toggleNode: undefined,
alignEnd: null,
align: null,
show: null,
drop: null,
});
Expand Down
18 changes: 9 additions & 9 deletions src/DropdownMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ export function useDropdownMenu(options = {}) {
} = options;

const show = context.show == null ? options.show : context.show;
const alignEnd =
context.alignEnd == null ? options.alignEnd : context.alignEnd;
const align = context.align == null ? options.align : context.align;

if (show && !hasShownRef.current) {
hasShownRef.current = true;
Expand All @@ -34,10 +33,11 @@ export function useDropdownMenu(options = {}) {

const { drop, setMenu, menuElement, toggleElement } = context;

let placement = alignEnd ? 'bottom-end' : 'bottom-start';
if (drop === 'up') placement = alignEnd ? 'top-end' : 'top-start';
else if (drop === 'right') placement = alignEnd ? 'right-end' : 'right-start';
else if (drop === 'left') placement = alignEnd ? 'left-end' : 'left-start';
let placement = 'bottom';
if (drop === 'up') placement = 'top';
else if (drop === 'right' || drop === 'left') placement = drop;
if (align === 'right') placement += `-end`;
else placement += `-${align || 'start'}`;

const popper = usePopper(toggleElement, menuElement, {
placement,
Expand All @@ -62,7 +62,7 @@ export function useDropdownMenu(options = {}) {
};
const childArgs = {
show,
alignEnd,
align,
hasShown: hasShownRef.current,
close: handleClose,
};
Expand Down Expand Up @@ -99,7 +99,7 @@ const propTypes = {
*
* @type {Function ({
* show: boolean,
* alignEnd: boolean,
* align: string,
* close: (?SyntheticEvent) => void,
* placement: Placement,
* outOfBoundaries: ?boolean,
Expand Down Expand Up @@ -129,7 +129,7 @@ const propTypes = {
* Generally this is provided by the parent `Dropdown` component,
* but may also be specified as a prop directly.
*/
alignEnd: PropTypes.bool,
align: PropTypes.string,

/**
* Enables the Popper.js `flip` modifier, allowing the Dropdown to
Expand Down
11 changes: 8 additions & 3 deletions test/DropdownSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,18 @@ describe('<Dropdown>', () => {
buttonNode.getAttribute('id').should.be.ok;
});

it('forwards alignEnd to menu', () => {
it('forwards align to menu', () => {
const renderSpy = sinon.spy(args => {
args.alignEnd.should.equal(true);
args.align.should.equal('center');
});

mount(
<SimpleDropdown show alignEnd usePopper={false} menuSpy={renderSpy} />,
<SimpleDropdown
show
align="center"
usePopper={false}
menuSpy={renderSpy}
/>,
);

renderSpy.should.have.been.called;
Expand Down
7 changes: 4 additions & 3 deletions www/src/examples/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ const Toggle = ({ id, children }) => {
);
};

const DropdownButton = ({ show, onToggle, drop, alignEnd, title, role }) => (
const DropdownButton = ({ show, onToggle, drop, align, title, role }) => (
<Dropdown
show={show}
onToggle={onToggle}
drop={drop}
alignEnd={alignEnd}
align={align}
itemSelector="button:not(:disabled)"
>
{({ props }) => (
Expand All @@ -70,7 +70,8 @@ class DropdownExample extends React.Component {
onToggle={nextShow => this.setState({ show: nextShow })}
title={`${show ? 'Close' : 'Open'} Dropdown`}
/>
<DropdownButton alignEnd title="Align right" />
<DropdownButton align="center" title="Align center" />
<DropdownButton align="right" title="Align right" />

<DropdownButton drop="up" title="Drop up" />
<DropdownButton role="menu" title="Role 'menu'" />
Expand Down