Skip to content

Commit

Permalink
fix: style fix, rewrite menu items with LinkControl
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Yuferev committed Apr 16, 2019
1 parent 7778fd1 commit c6b0320
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 85 deletions.
2 changes: 2 additions & 0 deletions packages/core/src/menu/MenuControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ export interface MenuControlProps<Item extends BaseMenuItem> {
export class MenuControl<Item extends BaseMenuItem> extends PureComponent<
MenuControlProps<Item>
> {

render() {
const {props} = this
return props.children({
levels: new MenuLevelBuilder(props.items).levels(props.selected),
})
}

}
8 changes: 5 additions & 3 deletions packages/core/src/menu/MenuLevelBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ export type MenuLevel<Item extends BaseMenuItem> = {
} | undefined

export class MenuLevelBuilder<Item extends BaseMenuItem> {

constructor(protected items: MenuTree<Item>[]) {}

protected buildLevels(
selected: string,
items: MenuTree<Item>[],
levels: MenuLevel<Item>[],
depth = 0,
parentActive = false
parentActive = false,
): boolean {
if (levels.length <= depth) levels.push(undefined)
let activeItem: MenuTree<Item> | undefined
Expand All @@ -36,7 +37,7 @@ export class MenuLevelBuilder<Item extends BaseMenuItem> {
sub,
levels,
depth + 1,
currentActive || parentActive
currentActive || parentActive,
)
: false
if (currentActive) activeItem = item
Expand All @@ -46,7 +47,7 @@ export class MenuLevelBuilder<Item extends BaseMenuItem> {
if (activeItem || parentActive) {
levels[depth] = {
items,
selected: activeItem ? activeItem.id : items[0].id
selected: activeItem ? activeItem.id : items[0].id,
}
}

Expand All @@ -64,4 +65,5 @@ export class MenuLevelBuilder<Item extends BaseMenuItem> {
this.buildLevels(selected, this.items, result)
return result
}

}
37 changes: 17 additions & 20 deletions packages/mobile/src/horizontal-menu/HorizontalMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
import {BaseMenuItem, ButtonControl, styled} from '@qiwi/pijma-core'
import {BaseMenuItem, styled, Flex, Card} from '@qiwi/pijma-core'
import React from 'react'

import {HorizontalMenuItem} from './HorizontalMenuItem'

// @see https://iamsteve.me/blog/entry/using-flexbox-for-horizontal-scrolling-navigation
export const HorizontalMenuContainer = styled('nav')(({theme}) => ({
display: 'flex',
flexWrap: 'nowrap',
const ScrollableFlex = styled(Flex)({
overflowX: 'auto',
webkitOverflowScrolling: 'auto',
msOverflowStyle: '-ms-autohiding-scrollbar',
'&::-webkit-scrollbar': {
display: 'none',
},
'&:last-child': {
marginRight: 0,
},
paddingLeft: '16px',
paddingRight: '16px',
borderBottom: '1px solid ' + theme.color.gray.light,
}))
})

export interface HorizontalMenuProps<Item extends BaseMenuItem = BaseMenuItem> {
/**
Expand All @@ -40,21 +32,26 @@ export const HorizontalMenu = <Item extends BaseMenuItem>({
selected,
id,
}: HorizontalMenuProps<Item>) => (
<HorizontalMenuContainer
<Card
id={id}
children={items.map(item => (
<ButtonControl
key={item.id}
onClick={onSelect.bind(null, item)}
children={renderProps => (
bb="1px solid #e6e6e6"
children={
<ScrollableFlex
id={`${id}-wrap`}
as="nav"
wrap="nowrap"
px={4}
children={items.map((item, index) => (
<HorizontalMenuItem
id={`${id}-${item.id}`}
active={selected === item.id}
onClick={renderProps.onClick}
key={item.id}
isLast={index === items.length - 1}
onClick={onSelect.bind(null, item)}
children={item.title}
/>
)}
))}
/>
))}
}
/>
)
67 changes: 43 additions & 24 deletions packages/mobile/src/horizontal-menu/HorizontalMenuItem.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,52 @@
import {styled} from '@qiwi/pijma-core'
import {Card, LinkControl, Lnk, Typo} from '@qiwi/pijma-core'
import React from 'react'

export interface HorizontalMenuItemProps {
active: boolean
id: string
href?: string
target?: string
reverse?: boolean
onClick?: (e: React.MouseEvent) => void
isLast?: boolean
onClick?: () => void
}

export const HorizontalMenuItem = styled('a', {
shouldForwardProp: prop => prop !== 'active',
})<HorizontalMenuItemProps>(({active, theme}) => ({
cursor: 'pointer',
fontFamily: theme.font.family,
fontWeight: theme.font.weight.normal,
fontSize: '16px',
textDecoration: 'none',
color: theme.color.black,
flexShrink: 0,
flexGrow: 0,
whiteSpace: 'nowrap',
paddingBottom: '4px',
marginRight: '24px',
borderBottomWidth: '4px',
borderBottomStyle: 'solid',
borderBottomColor: active ? theme.color.brand : 'transparent',
'&:hover, &:active, &:focus': {
textDecoration: 'none',
borderBottomColor: theme.color.brand,
},
}))
const CardLink = Card.withComponent(Lnk)

export const HorizontalMenuItem: React.FC<HorizontalMenuItemProps> = props => (
<LinkControl
onClick={props.onClick}
target={props.target}
children={renderProps => (
<CardLink
id={props.id}
href={props.href}
target={props.target}
onClick={renderProps.onClick}
onFocus={renderProps.onFocus}
onBlur={renderProps.onBlur}
onMouseEnter={renderProps.onMouseEnter}
onMouseLeave={renderProps.onMouseLeave}
onMouseUp={renderProps.onMouseUp}
onMouseDown={renderProps.onMouseDown}
cursor="pointer"
mr={props.isLast ? 0 : 6}
pb={1}
bb={`4px solid ${
renderProps.hover || props.active ? '#ff8c00' : 'transparent'
}`}
children={
<Typo
id={`${props.id}-text`}
as="span"
cursor="pointer"
size={4}
height={6}
weight={500}
children={props.children}
/>
}
/>
)}
/>
)
24 changes: 7 additions & 17 deletions packages/mobile/src/select-menu/SelectMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import {
BaseMenuItem,
ButtonControl,
Card,
Flex,
FlexItem,
Icon,
} from '@qiwi/pijma-core'
import {BaseMenuItem, Card, Flex, FlexItem, Icon} from '@qiwi/pijma-core'
import React, {Component} from 'react'

import {SelectMenuItem} from './SelectMenuItem'
Expand All @@ -31,6 +24,7 @@ export interface SelectMenuState {
export class SelectMenu<
Item extends BaseMenuItem = BaseMenuItem
> extends Component<SelectMenuProps<Item>, SelectMenuState> {

state: SelectMenuState = {
expanded: false,
}
Expand Down Expand Up @@ -78,22 +72,18 @@ export class SelectMenu<
direction="column"
children={items.map(item =>
item.id === selected ? null : (
<ButtonControl
<SelectMenuItem
key={item.id}
id={`${id}-item-${item.id}`}
onClick={toggle.bind(this, item)}
children={renderProps => (
<SelectMenuItem
id={`${id}-item-${item.id}`}
onClick={renderProps.onClick}
children={item.title}
/>
)}
children={item.title}
/>
)
),
)}
/>
)}
</Card>
)
}

}
61 changes: 40 additions & 21 deletions packages/mobile/src/select-menu/SelectMenuItem.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,48 @@
import {styled} from '@qiwi/pijma-core'
import {Card, LinkControl, Lnk, Typo} from '@qiwi/pijma-core'
import React from 'react'

export interface SelectMenuItemProps {
id: string
href?: string
target?: string
reverse?: boolean
onClick?: (e: React.MouseEvent) => void
onClick?: () => void
}

export const SelectMenuItem = styled('a')<
SelectMenuItemProps
>(({theme}) => ({
cursor: 'pointer',
fontFamily: theme.font.family,
fontWeight: theme.font.weight.normal,
fontSize: '16px',
textDecoration: 'none',
color: theme.color.black,
flexShrink: 0,
flexGrow: 0,
whiteSpace: 'nowrap',
lineHeight: '21px',
padding: '8px 16px',
'&:hover, &:active, &:focus': {
textDecoration: 'none',
backgroundColor: theme.color.gray.lightest,
},
}))
const CardLink = Card.withComponent(Lnk)

export const SelectMenuItem: React.FC<SelectMenuItemProps> = props => (
<LinkControl
onClick={props.onClick}
target={props.target}
children={renderProps => (
<CardLink
id={props.id}
href={props.href}
target={props.target}
onClick={renderProps.onClick}
onFocus={renderProps.onFocus}
onBlur={renderProps.onBlur}
onMouseEnter={renderProps.onMouseEnter}
onMouseLeave={renderProps.onMouseLeave}
onMouseUp={renderProps.onMouseUp}
onMouseDown={renderProps.onMouseDown}
cursor="pointer"
px={4}
py={2}
bg={`${renderProps.hover ? '#f5f5f5' : 'transparent'}`}
children={
<Typo
id={`${props.id}-text`}
weight={300}
as="span"
cursor="pointer"
size={4}
height={6}
children={props.children}
/>
}
/>
)}
/>
)

0 comments on commit c6b0320

Please sign in to comment.