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

feat(AppNaviDropdownMenuButton): 選択中の項目が視覚的にも伝わるように装飾を追加 #5193

Open
wants to merge 3 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,36 +1,65 @@
import React, { type FC, type PropsWithChildren, type ReactNode } from 'react'
import React, { type FC, type PropsWithChildren, ReactElement, type ReactNode } from 'react'
import { tv } from 'tailwind-variants'

import { DropdownMenuGroup } from '../Dropdown'
import { DropdownMenuButton } from '../Dropdown/DropdownMenuButton/DropdownMenuButton'

type AppNaviDropdownMenuButtonProps = PropsWithChildren<{
/** 引き金となるボタンラベル */
label: ReactNode
}>

const dropdownMenuButton = tv({
base: [
'smarthr-ui-AppNavi-dropdownMenuButton',
[
'[&_.smarthr-ui-DropdownMenuButton-trigger]:shr-border-none',
'[&_.smarthr-ui-DropdownMenuButton-trigger]:shr-px-0.5',
'[&_.smarthr-ui-DropdownMenuButton-trigger]:shr-text-grey',
'[&_.smarthr-ui-DropdownMenuButton-trigger]:shr-rounded-none',
const { trigger: triggerStyle, actionItem: actionItemStyle } = tv({
slots: {
trigger: [
'smarthr-ui-AppNavi-dropdownMenuButton',
[
'[&_.smarthr-ui-DropdownMenuButton-trigger]:shr-border-none',
'[&_.smarthr-ui-DropdownMenuButton-trigger]:shr-px-0.5',
'[&_.smarthr-ui-DropdownMenuButton-trigger]:shr-text-grey',
'[&_.smarthr-ui-DropdownMenuButton-trigger]:shr-rounded-none',
],
[
'[&_.smarthr-ui-DropdownMenuButton-trigger:has([aria-current=page])]:shr-relative',
'[&_.smarthr-ui-DropdownMenuButton-trigger:has([aria-current=page])]:shr-text-black',
],
[
'[&_.smarthr-ui-DropdownMenuButton-trigger:has([aria-current=page])]:after:shr-content-[""]',
'[&_.smarthr-ui-DropdownMenuButton-trigger:has([aria-current=page])]:after:shr-absolute',
'[&_.smarthr-ui-DropdownMenuButton-trigger:has([aria-current=page])]:after:shr-bottom-0',
'[&_.smarthr-ui-DropdownMenuButton-trigger:has([aria-current=page])]:after:shr-inset-x-0',
'[&_.smarthr-ui-DropdownMenuButton-trigger:has([aria-current=page])]:after:shr-h-0.25',
'[&_.smarthr-ui-DropdownMenuButton-trigger:has([aria-current=page])]:after:shr-bg-main',
],
],
[
'[&_.smarthr-ui-DropdownMenuButton-trigger:has([aria-current=page])]:shr-relative',
'[&_.smarthr-ui-DropdownMenuButton-trigger:has([aria-current=page])]:shr-text-black',
actionItem: [
'aria-current-page:shr-bg-grey-9 aria-current-page:shr-font-bold',
// aria-current-page より詳細度を確実に上げる
'[&&]:hover:shr-bg-head-darken',
],
[
'[&_.smarthr-ui-DropdownMenuButton-trigger:has([aria-current=page])]:after:shr-content-[""]',
'[&_.smarthr-ui-DropdownMenuButton-trigger:has([aria-current=page])]:after:shr-absolute',
'[&_.smarthr-ui-DropdownMenuButton-trigger:has([aria-current=page])]:after:shr-bottom-0',
'[&_.smarthr-ui-DropdownMenuButton-trigger:has([aria-current=page])]:after:shr-inset-x-0',
'[&_.smarthr-ui-DropdownMenuButton-trigger:has([aria-current=page])]:after:shr-h-0.25',
'[&_.smarthr-ui-DropdownMenuButton-trigger:has([aria-current=page])]:after:shr-bg-main',
],
],
})
},
})()

const renderItemList = (children: ReactNode) =>
React.Children.map(children, (item): ReactNode => {
if (!React.isValidElement(item)) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DropdownMenuButton の children は portal に展開されてしまいスタイリングをあてられないため、React.Children.mapReact.cloneElemtne を使ってスタイリングしています。

return null
}

if (item.type === React.Fragment) {
return renderItemList(item.props.children)
}

if (item.type === DropdownMenuGroup) {
return (
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DrpodownMenuGroup の children に対してもスタイリングを当てたいため、再帰的に呼び出し。

<DropdownMenuGroup {...item.props}>{renderItemList(item.props.children)}</DropdownMenuGroup>
)
}

return React.cloneElement(item as ReactElement, {
className: actionItemStyle({ className: item.props.className }),
})
})

export const AppNaviDropdownMenuButton: FC<AppNaviDropdownMenuButtonProps> = ({
label,
Expand All @@ -44,8 +73,8 @@ export const AppNaviDropdownMenuButton: FC<AppNaviDropdownMenuButtonProps> = ({
<span hidden>{children}</span>
</>
}
className={dropdownMenuButton()}
className={triggerStyle()}
>
{children}
{renderItemList(children)}
</DropdownMenuButton>
)
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ const Link: React.FC<{

export const Template: StoryFn<typeof AppNavi> = (args) => (
<AppNavi {...args}>
<AppNaviButton current>ボタン</AppNaviButton>
<AppNaviButton>ボタン</AppNaviButton>
<AppNaviAnchor href="/">アンカーボタン</AppNaviAnchor>
<AppNaviDropdownMenuButton label="ドロップダウンボタン">
<Button>ボタン</Button>
<AnchorButton href="#">アンカーボタン</AnchorButton>
<DropdownMenuGroup name="グループ">
<Button>権限</Button>
<Button aria-current="page">権限</Button>
<AnchorButton href="#">その他</AnchorButton>
</DropdownMenuGroup>
</AppNaviDropdownMenuButton>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { AppNavi } from '../AppNavi'

import { Template } from './AppNavi.stories'

import type { Meta, StoryObj } from '@storybook/react'
import type { Meta } from '@storybook/react'

export default {
title: 'Navigation(ナビゲーション)/AppNavi/VRT',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const dropdownMenuButton = tv({
],
],
actionListItemButton: [
'shr-justify-start shr-border-none shr-py-0.5 shr-font-normal',
'shr-justify-start shr-rounded-none shr-border-none shr-py-0.5 shr-font-normal',
'focus-visible:shr-focus-indicator--inner',
],
},
Expand Down
Loading