Skip to content

Commit

Permalink
feat(Menu): support rtl
Browse files Browse the repository at this point in the history
  • Loading branch information
youluna committed Nov 28, 2018
1 parent d863787 commit 5241d7b
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 22 deletions.
4 changes: 2 additions & 2 deletions docs/menu/demo/accordian.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Menu } from '@alifd/next';

const { SubMenu, Item } = Menu;

ReactDOM.render((
ReactDOM.render(
<Menu defaultOpenKeys="1" className="my-menu" openMode="single">
<SubMenu key="0" label="Sub menu 1">
<Item key="0-0">Sub option 1</Item>
Expand All @@ -42,7 +42,7 @@ ReactDOM.render((
<Item key="3-2">Sub option 3</Item>
</SubMenu>
</Menu>
), mountNode);
, mountNode);
````

````css
Expand Down
4 changes: 2 additions & 2 deletions docs/menu/demo/basic.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Menu } from '@alifd/next';

const { SubMenu, Item, Group, Divider } = Menu;

ReactDOM.render((
ReactDOM.render(
<Menu className="my-menu" defaultOpenKeys="sub-menu">
<Item key="1">Option 1</Item>
<Item disabled key="2">Disabled option 2</Item>
Expand Down Expand Up @@ -47,7 +47,7 @@ ReactDOM.render((
<a href="https://www.taobao.com/" target="__blank">Option Link</a>
</Item>
</Menu>
), mountNode);
, mountNode);
````

````css
Expand Down
4 changes: 2 additions & 2 deletions docs/menu/demo/hover.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Menu } from '@alifd/next';

const { SubMenu, Item, Divider } = Menu;

ReactDOM.render((
ReactDOM.render(
<Menu className="my-menu" mode="popup" triggerType="hover">
<Item key="1">Option 1</Item>
<Item key="2">Option 2</Item>
Expand All @@ -34,7 +34,7 @@ ReactDOM.render((
<Item key="popup-2-2">Popup option 2</Item>
</SubMenu>
</Menu>
), mountNode);
, mountNode);
````

````css
Expand Down
4 changes: 2 additions & 2 deletions docs/menu/demo/hoz.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Menu } from '@alifd/next';

const { SubMenu, Item } = Menu;

ReactDOM.render((
ReactDOM.render(
<Menu header="aaa" direction="hoz" mode="popup" className="my-hoz-menu" popupClassName="my-hoz-menu" popupAutoWidth>
<Item key="1">First</Item>
<Item key="2">Second</Item>
Expand All @@ -33,7 +33,7 @@ ReactDOM.render((
</SubMenu>
<Item key="3">Third</Item>
</Menu>
), mountNode);
, mountNode);
````

````css
Expand Down
4 changes: 2 additions & 2 deletions docs/menu/demo/popup-align.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Menu } from '@alifd/next';

const { SubMenu, Item, Divider } = Menu;

ReactDOM.render((
ReactDOM.render(
<Menu className="my-menu" mode="popup" popupAlign="outside">
<Item key="1">Option 1</Item>
<Item key="2">Option 2</Item>
Expand All @@ -34,7 +34,7 @@ ReactDOM.render((
<Item key="popup-2-2">Popup option 2</Item>
</SubMenu>
</Menu>
), mountNode);
, mountNode);
````

````css
Expand Down
4 changes: 2 additions & 2 deletions docs/menu/demo/popup.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Menu } from '@alifd/next';

const { SubMenu, Item, Divider } = Menu;

ReactDOM.render((
ReactDOM.render(
<Menu className="my-menu" mode="popup">
<Item key="1">Option 1</Item>
<Item key="2">Option 2</Item>
Expand All @@ -34,7 +34,7 @@ ReactDOM.render((
<Item key="popup-2-2">Popup option 2</Item>
</SubMenu>
</Menu>
), mountNode);
, mountNode);
````

````css
Expand Down
1 change: 1 addition & 0 deletions src/menu/main.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@import "../core/index-noreset.scss";
@import "scss/variable";
@import "scss/mixin";
@import "./rtl.scss";

#{$menu-prefix} {
@include box-sizing;
Expand Down
38 changes: 38 additions & 0 deletions src/menu/rtl.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#{$menu-prefix}[dir="rtl"] {
#{$menu-prefix} {
&-item {
&-helper {
float: left;
}

.#{$css-prefix}checkbox,
.#{$css-prefix}radio {
margin-left: $menu-icon-margin;
margin-right: 0;
}
}

&-hoz-right {
float: right;
}

&-icon-arrow.#{$css-prefix}icon {
left: 10px;
right: auto;
}

&-hoz-icon-arrow.#{$css-prefix}icon {
left: 6px;
right: auto;
}

&-icon-selected.#{$css-prefix}icon {
@include icon-size($menu-icon-selected-size, 0, -($menu-padding-horizontal + $menu-icon-selected-size) / 2);
}

}
#{$menu-prefix}-icon-arrow.#{$css-prefix}icon {
left: 10px;
right: auto;
}
}
13 changes: 10 additions & 3 deletions src/menu/view/create.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,17 @@ export default function create(props) {
afterClose && afterClose();
};

const newContext = ConfigProvider.getContext();

let menu;
render(<ContextMenu afterClose={closeChain} {...others} />, div, function() {
menu = this;
});
render(
<ConfigProvider {...newContext}>
<ContextMenu afterClose={closeChain} {...others} />
</ConfigProvider>
, div, function() {
menu = this;
}
);

menuInstance = {
destroy: () => {
Expand Down
4 changes: 2 additions & 2 deletions src/menu/view/item.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export default class Item extends Component {
const { level, root, replaceClassName, groupIndent, component, disabled, className, children, needIndent, parentMode, _key } = this.props;
const others = pickOthers(Object.keys(Item.propTypes), this.props);

const { prefix, focusable, inlineIndent, itemClassName } = root.props;
const { prefix, focusable, inlineIndent, itemClassName, rtl } = root.props;
const focused = this.getFocused();

const newClassName = replaceClassName ? className : cx({
Expand All @@ -153,7 +153,7 @@ export default class Item extends Component {
if (parentMode === 'inline' && level > 1 && inlineIndent > 0 && needIndent) {
others.style = {
...(others.style || {}),
paddingLeft: `${(level * inlineIndent) - ((groupIndent || 0) * 0.4 * inlineIndent)}px`
[rtl ? 'paddingRight' : 'paddingLeft']: `${(level * inlineIndent) - ((groupIndent || 0) * 0.4 * inlineIndent)}px`
};
}
const TagName = component;
Expand Down
7 changes: 6 additions & 1 deletion src/menu/view/menu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default class Menu extends Component {
static propTypes = {
prefix: PropTypes.string,
pure: PropTypes.bool,
rtl: PropTypes.bool,
className: PropTypes.string,
/**
* 菜单项和子菜单
Expand Down Expand Up @@ -607,7 +608,7 @@ export default class Menu extends Component {
}

render() {
const { prefix, className, direction, hozAlign, header, footer, selectMode } = this.props;
const { prefix, className, direction, hozAlign, header, footer, selectMode, rtl } = this.props;
const others = pickOthers(Object.keys(Menu.propTypes), this.props);

const newClassName = cx({
Expand All @@ -623,6 +624,10 @@ export default class Menu extends Component {
const footerElement = footer ? <li className={`${prefix}menu-footer`}>{footer}</li> : null;
const shouldWrapItemsAndFooter = hozAlign === 'right' && !!header;

if (rtl) {
others.dir = 'rtl';
}

return (
<ul role={role} className={newClassName} onKeyDown={this.handleEnter} aria-multiselectable={selectMode === 'multiple'} {...others}>
{headerElement}
Expand Down
1 change: 1 addition & 0 deletions src/menu/view/popup-item.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ export default class PopupItem extends Component {

const positionProps = {};
let arrowProps;

if (direction === 'hoz' && level === 1) {
positionProps.align = 'tl bl';
positionProps.offset = [0, 0];
Expand Down
8 changes: 4 additions & 4 deletions src/menu/view/sub-menu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export default class SubMenu extends Component {

renderInline() {
const { _key, level, root, className, selectable: selectableFromProps, label, children, subMenuContentClassName, triggerType: propsTriggerType, parentMode } = this.props;
const { prefix, selectMode, triggerType: rootTriggerType, inlineArrowDirection, expandAnimation } = root.props;
const { prefix, selectMode, triggerType: rootTriggerType, inlineArrowDirection, expandAnimation, rtl } = root.props;
const triggerType = propsTriggerType || rootTriggerType;
const open = this.getOpen();
const others = obj.pickOthers(Object.keys(SubMenu.propTypes), this.props);
Expand Down Expand Up @@ -174,7 +174,7 @@ export default class SubMenu extends Component {
});

const subMenu = open ? (
<ul role="menu" ref="subMenu" className={newSubMenuContentClassName}>
<ul role="menu" dir={rtl ? 'rtl' : undefined} ref="subMenu" className={newSubMenuContentClassName}>
{this.passParentToChildren(children)}
</ul>
) : null;
Expand All @@ -198,7 +198,7 @@ export default class SubMenu extends Component {
renderPopup() {
const { children, subMenuContentClassName, ...others } = this.props;
const root = this.props.root;
const { prefix, popupClassName, popupStyle } = root.props;
const { prefix, popupClassName, popupStyle, rtl } = root.props;

const newClassName = cx({
[`${prefix}menu`]: true,
Expand All @@ -209,7 +209,7 @@ export default class SubMenu extends Component {

return (
<PopupItem {...others} hasSubMenu>
<ul role="menu" className={newClassName} style={popupStyle}>
<ul role="menu" dir={rtl ? 'rtl' : undefined} className={newClassName} style={popupStyle}>
{this.passParentToChildren(children)}
</ul>
</PopupItem>
Expand Down

0 comments on commit 5241d7b

Please sign in to comment.