Skip to content

Commit

Permalink
feat: support menu divider
Browse files Browse the repository at this point in the history
  • Loading branch information
drl990114 committed Nov 4, 2024
1 parent 9735dbf commit d519427
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 20 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zens",
"version": "0.0.36",
"version": "0.0.38",
"description": "MarkFlowy's ui component library.",
"keywords": [],
"homepage": "https://github.com/drl990114/zens#readme",
Expand Down Expand Up @@ -73,7 +73,7 @@
"react-dom": "^18.2.0",
"react-spinners": "^0.13.8",
"sonner": "^1.4.0",
"styled-components": "^6.1.11",
"styled-components": "^6.1.13",
"stylis": "^4.0.0"
},
"devDependencies": {
Expand Down
7 changes: 5 additions & 2 deletions src/Menu/demo/basic.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Menu } from 'zens';
import { Menu, MenuItemData } from 'zens';

export default () => {
const menuData = [
const menuData: MenuItemData[] = [
{
label: 'menu1',
value: 'menu1',
Expand All @@ -10,6 +10,9 @@ export default () => {
console.log('menu1');
},
},
{
type: 'divider',
},
{
label: 'menu2',
value: 'menu2',
Expand Down
20 changes: 18 additions & 2 deletions src/Menu/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import type { MenuProps as AkMenuProps } from '@ariakit/react';
import { MenuButton, MenuButtonArrow, MenuProvider, useMenuStore } from '@ariakit/react';

import { MenuItem, MenuItemCheckIcon, MenuWrapper, MenuSeparator } from './styles';

import Button from '../Button';
import { MenuItem, MenuItemCheckIcon, MenuWrapper } from './styles';

export { MenuProvider } from '@ariakit/react';
export * from './styles';
export { useMenuStore };

export type MenuItemData = {
export type MenuItemData = MenuGroupType | MenuDividerType;

export type MenuGroupType = {
label: string;
keybinding?: string;
value: string;
Expand All @@ -16,12 +20,20 @@ export type MenuItemData = {
children?: MenuItemData[];
};

export type MenuDividerType = {
type: 'divider';
};

interface MenuProps extends AkMenuProps {
menuButtonProps?: React.ComponentProps<typeof Button>;
triggerBtnClass?: string;
items: MenuItemData[];
}

export const isDivider = (item: MenuItemData): item is MenuDividerType => {
return (item as MenuDividerType)?.type === 'divider';
}

const Menu = (props: MenuProps) => {
const {
open,
Expand All @@ -36,6 +48,10 @@ const Menu = (props: MenuProps) => {

const renderItems = (menuItems: MenuItemData[]) => {
return menuItems.map((item) => {
if (isDivider(item)) {
return <MenuSeparator />
}

const key = item.value;

if (item.children && item.children?.length > 0) {
Expand Down
27 changes: 17 additions & 10 deletions src/Menu/styles.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as Ariakit from '@ariakit/react';
import styled from 'styled-components';

import * as Ariakit from '@ariakit/react';

import { darken } from '../Theme';

export const MenuItem = styled(Ariakit.MenuItem)`
Expand Down Expand Up @@ -48,16 +50,21 @@ export const MenuWrapper = styled(Ariakit.Menu)`
outline: none;
overflow: visible;
.separator {
width: 100%;
height: 0px;
margin-top: 0.5rem;
margin-bottom: 0.5rem;
border-color: ${(props) => props.theme.borderColor};
border-top-width: 1px;
}
.menu-label {
flex: 1 1 0%;
}
`;

export const MenuSeparator = styled(Ariakit.MenuSeparator)`
margin-top: 0.5rem;
margin-bottom: 0.5rem;
height: 0px;
width: 100%;
background-color: ${(props) => props.theme.contextMenuBgColor};
border-top-width: 1px;
border-bottom: none;
border-right: none;
border-left: none;
border-color: ${(props) => props.theme.contextMenuSeparatorColor};
color: ${(props) => props.theme.contextMenuSeparatorColor};
`;
1 change: 1 addition & 0 deletions src/Theme/dark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const styledDarkTheme = {
// components
contextMenuBgColor: '#18191B',
contextMenuBgColorHover: '#2D3134',
contextMenuSeparatorColor: '#919191',
buttonBgColor: '#21262c',
tooltipBgColor: '#43414A',
dialogBgColor: '#151515',
Expand Down
1 change: 1 addition & 0 deletions src/Theme/light.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const styledLightTheme = {
// components
contextMenuBgColor: '#FFFFFF',
contextMenuBgColorHover: '#E8E8EC',
contextMenuSeparatorColor: '#ffffff66',
buttonBgColor: '#f6f7f9',
tooltipBgColor: '#d7d7dc',
dialogBgColor: '#f6f7f9',
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17761,10 +17761,10 @@ style-to-object@^0.4.1:
dependencies:
inline-style-parser "0.1.1"

styled-components@^6.1.11:
version "6.1.11"
resolved "https://registry.yarnpkg.com/styled-components/-/styled-components-6.1.11.tgz#01948e5195bf1d39e57e0a85b41958c80e40cfb8"
integrity sha512-Ui0jXPzbp1phYij90h12ksljKGqF8ncGx+pjrNPsSPhbUUjWT2tD1FwGo2LF6USCnbrsIhNngDfodhxbegfEOA==
styled-components@^6.1.13:
version "6.1.13"
resolved "https://registry.yarnpkg.com/styled-components/-/styled-components-6.1.13.tgz#2d777750b773b31469bd79df754a32479e9f475e"
integrity sha512-M0+N2xSnAtwcVAQeFEsGWFFxXDftHUD7XrKla06QbpUMmbmtFBMMTcKWvFXtWxuD5qQkB8iU5gk6QASlx2ZRMw==
dependencies:
"@emotion/is-prop-valid" "1.2.2"
"@emotion/unitless" "0.8.1"
Expand Down

0 comments on commit d519427

Please sign in to comment.