@contember/create 2.0.0-beta.2 → 2.0.0-beta.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. package/package.json +2 -2
  2. package/resources/templates/template-workspace/admin/lib/datagrid/grid.tsx +5 -6
  3. package/resources/templates/template-workspace/admin/lib/datagrid/pagination.tsx +38 -14
  4. package/resources/templates/template-workspace/admin/lib/datagrid/table.tsx +13 -10
  5. package/resources/templates/template-workspace/admin/lib/datagrid/toolbar.tsx +22 -18
  6. package/resources/templates/template-workspace/admin/lib/datagrid/ui.tsx +29 -26
  7. package/resources/templates/template-workspace/admin/lib/form/container.tsx +5 -8
  8. package/resources/templates/template-workspace/admin/lib/form/inputs.tsx +5 -6
  9. package/resources/templates/template-workspace/admin/lib/form/labels.tsx +8 -0
  10. package/resources/templates/template-workspace/admin/lib/layout/index.ts +1 -0
  11. package/resources/templates/template-workspace/admin/lib/layout/layout-boxed.tsx +184 -0
  12. package/resources/templates/template-workspace/admin/lib/layout/layout.tsx +80 -160
  13. package/resources/templates/template-workspace/admin/lib/layout/slots.tsx +1 -0
  14. package/resources/templates/template-workspace/admin/lib/select/ui.tsx +1 -1
  15. package/resources/templates/template-workspace/admin/lib/ui/alert-dialog.tsx +6 -4
  16. package/resources/templates/template-workspace/admin/lib/ui/avatar.tsx +42 -0
  17. package/resources/templates/template-workspace/admin/lib/ui/collapsible.tsx +7 -5
  18. package/resources/templates/template-workspace/admin/lib/ui/dropdown.tsx +18 -16
  19. package/resources/templates/template-workspace/admin/lib/ui/menu.tsx +102 -74
  20. package/resources/templates/template-workspace/admin/lib/ui/popover.tsx +2 -2
  21. package/resources/templates/template-workspace/admin/lib/ui/progress.tsx +16 -18
  22. package/resources/templates/template-workspace/admin/lib/ui/separator.tsx +13 -0
  23. package/resources/templates/template-workspace/admin/lib/ui/sidebar.tsx +560 -0
  24. package/resources/templates/template-workspace/admin/lib/ui/skeleton.tsx +3 -0
  25. package/resources/templates/template-workspace/admin/lib/ui/switch.tsx +4 -5
  26. package/resources/templates/template-workspace/admin/lib/ui/toggle-group.tsx +2 -3
  27. package/resources/templates/template-workspace/admin/lib/ui/toggle.tsx +1 -2
  28. package/resources/templates/template-workspace/admin/lib/ui/tooltip.tsx +2 -2
  29. package/resources/templates/template-workspace/admin/lib/utils/uic.tsx +40 -6
  30. package/resources/templates/template-workspace/admin/lib/utils/use-mobile.ts +28 -0
@@ -1,168 +1,88 @@
1
- import { LogOutIcon, Maximize2Icon, MenuIcon, Minimize2Icon, PanelLeftCloseIcon, PanelLeftOpenIcon, PanelRightCloseIcon, PanelRightOpenIcon } from 'lucide-react'
2
- import { PropsWithChildren, useEffect, useState } from 'react'
1
+ import { Link } from '@contember/interface'
3
2
  import { useHasActiveSlotsFactory } from '@contember/react-slots'
4
- import { uic } from '../utils/uic'
3
+ import { dataAttribute } from '@contember/utilities'
4
+ import { PropsWithChildren } from 'react'
5
+ import { Separator } from '../ui/separator'
6
+ import {
7
+ Sidebar,
8
+ SidebarContent,
9
+ SidebarFooter,
10
+ SidebarGroup,
11
+ SidebarGroupContent,
12
+ SidebarHeader,
13
+ SidebarInset,
14
+ SidebarInsetContent,
15
+ SidebarInsetHeader,
16
+ SidebarInsetHeaderActions,
17
+ SidebarLayout,
18
+ SidebarMenuButton,
19
+ SidebarProvider,
20
+ SidebarRail,
21
+ SidebarTrigger,
22
+ } from '../ui/sidebar'
5
23
  import { SlotTargets } from './slots'
6
- import { Button } from '../ui/button'
7
- import { LogoutTrigger } from '@contember/react-identity'
8
- import { dict } from '../dict'
9
- import { useCurrentRequest } from '@contember/interface'
10
- import { useStoredState } from '@contember/react-utils'
11
24
 
12
- const LayoutBodyUI = uic('div', { baseClass: 'bg-gray-50 h-full min-h-screen relative py-4 pl-[calc(100vw-100%)]' })
13
- const LayoutMaxWidthUI = uic('div', {
14
- baseClass: 'mx-auto transition-all',
15
- variants: {
16
- layout: {
17
- stretch: 'max-w-[calc(100vw-5rem)]',
18
- default: 'max-w-[100rem]',
19
- },
20
- },
21
- defaultVariants: {
22
- layout: 'default',
23
- },
24
- })
25
- const LayoutBoxUI = uic('div', { baseClass: 'rounded-xl shadow-lg border bg-white gap-1 flex flex-col lg:flex-row mt-4 relative min-h-[calc(100vh-10rem)]' })
26
-
27
- const LayoutCenterPanelUI = uic('div', { baseClass: 'flex flex-col flex-2 p-4 gap-2 w-full flex-auto overflow-hidden' })
28
- const LayoutCenterTopUI = uic('div', { baseClass: 'flex justify-between pb-4 mb-4 border-b' })
29
-
30
- const LayoutLeftSidebarUI = uic('div', {
31
- baseClass: 'flex-col lg:border-r bg-neutral-50 border-r-gray-300 lg:w-96 flex-auto gap-2 relative rounded-l-xl',
32
- variants: {
33
- visibility: {
34
- show: 'flex',
35
- hidden: 'hidden',
36
- auto: 'hidden lg:flex',
37
- },
38
- },
39
- defaultVariants: {
40
- visibility: 'auto',
41
- },
42
- })
43
- const LayoutRightSidebarUI = uic('div', {
44
- baseClass: ' flex-col p-4 pt-6 gap-2 lg:border-l border-l-gray-300 lg:w-96 flex-auto relative',
45
- variants: {
46
- visibility: {
47
- show: 'flex',
48
- hidden: 'hidden',
49
- },
50
- },
51
- defaultVariants: {
52
- visibility: 'show',
53
- },
54
- })
55
-
56
- const LayoutTitleUI = uic('h1', { baseClass: 'text-2xl font-bold' })
57
-
58
- const LayoutFooterUI = uic('div', { baseClass: 'flex justify-end mt-2 mx-4' })
59
-
60
- const LayoutLeftPanelCloserUI = uic('a', { baseClass: 'hidden lg:flex self-end absolute top-1 right-1 opacity-0 text-gray-400 hover:opacity-100 transition-opacity cursor-pointer' })
61
- const LayoutLeftPanelOpenerUI = uic('a', { baseClass: 'hidden lg:block absolute top-1 left-1' })
62
- const LayoutRightPanelCloserUI = uic('a', { baseClass: 'hidden lg:flex self-end absolute top-1 right-1 opacity-0 text-gray-400 hover:opacity-100 transition-opacity cursor-pointer' })
63
- const LayoutRightPanelOpenerUI = uic('a', { baseClass: 'absolute top-1 right-1' })
64
- const LayoutSwitcherUI = uic('a', { baseClass: 'hidden lg:flex self-end absolute top-1 right-1 opacity-20 text-gray-400 hover:opacity-100 transition-opacity cursor-pointer' })
65
- const LayoutContentWrapperUI = uic('div', { baseClass: 'flex flex-col gap-12' })
66
-
67
- export const LayoutComponent = ({ children, ...rest }: PropsWithChildren<{}>) => {
25
+ export const LayoutComponent = ({ children }: PropsWithChildren) => {
68
26
  const isActive = useHasActiveSlotsFactory()
27
+ const hasRightSidebar = isActive('Sidebar')
69
28
 
70
- const [leftSidebarVisibility, setLeftSidebarVisibility] = useState<'show' | 'hidden' | 'auto'>('auto')
71
- const [layout, setLayout] = useStoredState<'default' | 'stretch'>('local', ['', 'layout'], it => it ?? 'default')
72
-
73
- const request = useCurrentRequest()
74
- useEffect(() => {
75
- setLeftSidebarVisibility(it => it === 'show' ? 'auto' : it)
76
- }, [request])
77
-
78
- const [rightSidebarVisibility, setRightSidebarVisibility] = useState<'show' | 'hidden'>('show')
79
-
80
- const hasRightSidebar = isActive('SidebarRightHeader', 'SidebarRightBody', 'SidebarRightFooter', 'Sidebar')
81
29
  return (
82
- <LayoutBodyUI>
83
- <LayoutSwitcherUI className="absolute top-0.5 right-0.5" onClick={() => setLayout(it => it === 'default' ? 'stretch' : 'default')}>
84
- {layout === 'default' ? <Maximize2Icon /> : <Minimize2Icon />}
85
- </LayoutSwitcherUI>
86
-
87
-
88
- <LayoutMaxWidthUI layout={layout}>
89
- <LayoutBoxUI>
90
- {leftSidebarVisibility === 'hidden' && (
91
- <LayoutLeftPanelOpenerUI onClick={() => setLeftSidebarVisibility('auto')}>
92
- <PanelLeftOpenIcon className="w-4 h-4" />
93
- </LayoutLeftPanelOpenerUI>
94
- )}
95
- {hasRightSidebar && rightSidebarVisibility === 'hidden' ? (
96
- <LayoutRightPanelOpenerUI onClick={() => setRightSidebarVisibility('show')}>
97
- <PanelRightOpenIcon className="w-4 h-4" />
98
- </LayoutRightPanelOpenerUI>
99
- ) : null}
100
-
101
- <LayoutLeftSidebarUI visibility={leftSidebarVisibility}>
102
-
103
- <LayoutLeftPanelCloserUI onClick={() => setLeftSidebarVisibility('hidden')}>
104
- <PanelLeftCloseIcon className="w-4 h-4" />
105
- </LayoutLeftPanelCloserUI>
106
-
107
- <div className={'p-4 flex gap-2'}>
108
- <SlotTargets.Logo />
109
- </div>
110
-
111
- <div className="px-4">
112
- <SlotTargets.Navigation />
113
- </div>
114
-
115
- <div className={'mt-auto rounded-bl py-2 px-2 border-t'}>
116
- <LogoutTrigger>
117
- <Button variant={'ghost'} size="sm" className="gap-2 hover:underline">
118
- <LogOutIcon className="w-3 h-3 text-gray-500" /> {dict.logout}
119
- </Button>
120
- </LogoutTrigger>
121
- </div>
122
-
123
- </LayoutLeftSidebarUI>
124
-
125
-
126
- <LayoutCenterPanelUI>
127
- <LayoutCenterTopUI>
128
- <div className={'flex gap-2'}>
129
- <SlotTargets.Back />
130
- <SlotTargets.Title as={LayoutTitleUI} />
131
- </div>
132
- <SlotTargets.ContentHeader />
133
- <div className={'flex gap-2'}>
134
- <SlotTargets.Actions />
135
- <div className={'flex flex-col lg:hidden p-4 gap-2 w-full flex-auto'}>
136
- <a onClick={() => setLeftSidebarVisibility(it => it !== 'show' ? 'show' : 'auto')}><MenuIcon /></a>
137
- </div>
138
- </div>
139
- </LayoutCenterTopUI>
140
-
141
- <LayoutContentWrapperUI>
142
- {children}
143
- </LayoutContentWrapperUI>
144
- </LayoutCenterPanelUI>
145
- {hasRightSidebar ?
146
- <LayoutRightSidebarUI visibility={rightSidebarVisibility ? 'show' : 'hidden'}>
147
- <LayoutRightPanelCloserUI onClick={() => setRightSidebarVisibility('hidden')}>
148
- <PanelRightCloseIcon />
149
- </LayoutRightPanelCloserUI>
150
- <div>
151
- <SlotTargets.Sidebar />
152
- </div>
153
- </LayoutRightSidebarUI>
154
- : null
155
- }
156
-
157
- </LayoutBoxUI>
158
-
159
- <LayoutFooterUI>
160
- <div>
161
- <SlotTargets.Footer />
162
- </div>
163
- </LayoutFooterUI>
164
- </LayoutMaxWidthUI>
165
- </LayoutBodyUI>
30
+ <SidebarProvider>
31
+ <SidebarLayout>
32
+ <Sidebar collapsible="icon" side="left">
33
+ <SidebarHeader>
34
+ <Link to="index">
35
+ <SidebarMenuButton
36
+ size="lg"
37
+ className="data-[state=open]:bg-sidebar-accent data-[state=open]:text-sidebar-accent-foreground"
38
+ data-active={dataAttribute(false)}
39
+ >
40
+ <SlotTargets.Logo />
41
+ </SidebarMenuButton>
42
+ </Link>
43
+ </SidebarHeader>
44
+ <SidebarContent>
45
+ <SidebarGroup>
46
+ <SidebarGroupContent>
47
+ <SlotTargets.Navigation />
48
+ </SidebarGroupContent>
49
+ </SidebarGroup>
50
+ </SidebarContent>
51
+ <SidebarFooter>
52
+ <SlotTargets.UserNavigation />
53
+ </SidebarFooter>
54
+ <SidebarRail />
55
+ </Sidebar>
56
+
57
+ <SidebarInset className="max-h-screen overflow-hidden">
58
+ <SidebarInsetHeader>
59
+ <SidebarInsetHeaderActions>
60
+ <SidebarTrigger className="-ml-1" />
61
+
62
+ <Separator orientation="vertical" className="mr-2 h-4" />
63
+
64
+ <SlotTargets.Back />
65
+ <SlotTargets.Title />
66
+ </SidebarInsetHeaderActions>
67
+ <SidebarInsetHeaderActions>
68
+ <SlotTargets.Actions />
69
+ </SidebarInsetHeaderActions>
70
+ </SidebarInsetHeader>
71
+
72
+ <SidebarInsetContent>
73
+ {children}
74
+ </SidebarInsetContent>
75
+ </SidebarInset>
76
+
77
+ {hasRightSidebar && (
78
+ <SidebarProvider>
79
+ <Sidebar collapsible="icon" side="right" variant="inset">
80
+ <SlotTargets.Sidebar />
81
+ <SidebarRail />
82
+ </Sidebar>
83
+ </SidebarProvider>
84
+ )}
85
+ </SidebarLayout>
86
+ </SidebarProvider>
166
87
  )
167
88
  }
168
- LayoutComponent.displayName = 'Layout'
@@ -12,6 +12,7 @@ export const [, Slots, SlotTargets] = createSlotComponents([
12
12
  'ContentHeader',
13
13
  'Sidebar',
14
14
  'Actions',
15
+ 'UserNavigation',
15
16
  ])
16
17
  export const Title = memo<{ children: ReactNode }>(({ children }) => {
17
18
  const titleText = useChildrenAsLabel(children)
@@ -34,7 +34,7 @@ export const SelectInputActionsUI = uic('span', {
34
34
  })
35
35
 
36
36
  export const SelectListItemUI = uic(Button, {
37
- baseClass: 'w-full text-left justify-start gap-1 data-[highlighted]:bg-gray-200 data-[selected]:bg-gray-100 group relative',
37
+ baseClass: 'w-full text-left justify-start gap-1 data-[highlighted]:bg-gray-200 data-[selected]:bg-gray-100 group relative h-auto',
38
38
  defaultProps: {
39
39
  variant: 'ghost',
40
40
  size: 'sm',
@@ -1,11 +1,13 @@
1
- import * as React from 'react'
2
1
  import * as AlertDialogPrimitive from '@radix-ui/react-alert-dialog'
3
- import { buttonConfig } from './button'
2
+ import * as React from 'react'
4
3
  import { uic } from '../utils'
5
4
  import { createComponentOpenHooks } from '../utils/createComponentOpenHooks'
5
+ import { buttonConfig } from './button'
6
6
 
7
-
8
- export const { Component: AlertDialog, useOpen: useAlertDialogOpenState } = createComponentOpenHooks(AlertDialogPrimitive.Root)
7
+ export const {
8
+ Component: AlertDialog,
9
+ useOpen: useAlertDialogOpenState,
10
+ } = createComponentOpenHooks(AlertDialogPrimitive.Root)
9
11
 
10
12
  export const AlertDialogTrigger = AlertDialogPrimitive.Trigger
11
13
 
@@ -0,0 +1,42 @@
1
+ import * as AvatarPrimitive from '@radix-ui/react-avatar'
2
+ import { ComponentPropsWithoutRef, ElementRef, forwardRef } from 'react'
3
+ import { cn, uic } from '../utils'
4
+
5
+ export const Avatar = uic(AvatarPrimitive.Root, {
6
+ baseClass: 'relative flex h-10 w-10 shrink-0 overflow-hidden rounded-full',
7
+ displayName: AvatarPrimitive.Root.displayName,
8
+ })
9
+
10
+ export const AvatarImage = uic(AvatarPrimitive.Image, {
11
+ baseClass: 'aspect-square h-full w-full',
12
+ displayName: AvatarPrimitive.Image.displayName,
13
+ })
14
+
15
+ const useGenerateAvatarFallbackColor = (name?: string) => {
16
+ if (!name) return 'hsl(0, 80%, 80%)'
17
+
18
+ const charCodes = name.split('').map(char => char.charCodeAt(0))
19
+ const sum = charCodes.reduce((acc, code) => acc + code, 0)
20
+ const hue = sum % 360
21
+ return `hsl(${hue}, 80%, 80%)`
22
+ }
23
+
24
+ export const AvatarFallback = forwardRef<
25
+ ElementRef<typeof AvatarPrimitive.Fallback>,
26
+ ComponentPropsWithoutRef<typeof AvatarPrimitive.Fallback> & { avatarFallbackColorString?: string }
27
+ >(({ className, avatarFallbackColorString, ...props }, ref) => {
28
+ const backgroundColor = useGenerateAvatarFallbackColor(avatarFallbackColorString)
29
+
30
+ return (
31
+ <AvatarPrimitive.Fallback
32
+ ref={ref}
33
+ style={{ backgroundColor }}
34
+ className={cn(
35
+ 'flex h-full w-full items-center justify-center rounded-full bg-muted text-sm font-bold',
36
+ className,
37
+ )}
38
+ {...props}
39
+ />
40
+ )
41
+ })
42
+ AvatarFallback.displayName = AvatarPrimitive.Fallback.displayName
@@ -1,9 +1,11 @@
1
1
  import * as CollapsiblePrimitive from '@radix-ui/react-collapsible'
2
+ import { createComponentOpenHooks } from '../utils/createComponentOpenHooks'
2
3
 
3
- const Collapsible = CollapsiblePrimitive.Root
4
+ export const {
5
+ Component: Collapsible,
6
+ useOpen: useCollapsibleOpenState,
7
+ } = createComponentOpenHooks(CollapsiblePrimitive.Root)
4
8
 
5
- const CollapsibleTrigger = CollapsiblePrimitive.CollapsibleTrigger
9
+ export const CollapsibleTrigger = CollapsiblePrimitive.CollapsibleTrigger
6
10
 
7
- const CollapsibleContent = CollapsiblePrimitive.CollapsibleContent
8
-
9
- export { Collapsible, CollapsibleTrigger, CollapsibleContent }
11
+ export const CollapsibleContent = CollapsiblePrimitive.CollapsibleContent
@@ -1,12 +1,15 @@
1
- import * as React from 'react'
2
- import { ReactNode } from 'react'
3
1
  import * as DropdownMenuPrimitive from '@radix-ui/react-dropdown-menu'
4
2
  import { Check, ChevronRight, Circle, MoreHorizontalIcon } from 'lucide-react'
5
- import { cn, uic } from '../utils'
6
- import { buttonConfig } from './button'
3
+ import * as React from 'react'
4
+ import { ReactNode } from 'react'
5
+ import { uic } from '../utils'
7
6
  import { createComponentOpenHooks } from '../utils/createComponentOpenHooks'
7
+ import { buttonConfig } from './button'
8
8
 
9
- export const { Component: DropdownMenu, useOpen: useDropdownMenuOpenState } = createComponentOpenHooks(DropdownMenuPrimitive.Root)
9
+ export const {
10
+ Component: DropdownMenu,
11
+ useOpen: useDropdownMenuOpenState,
12
+ } = createComponentOpenHooks(DropdownMenuPrimitive.Root)
10
13
 
11
14
  export const DropdownMenuTrigger = DropdownMenuPrimitive.Trigger
12
15
 
@@ -85,7 +88,7 @@ export const DropdownMenuShortcut = uic('span', {
85
88
 
86
89
  export const DropDownTriggerButton = uic('button', {
87
90
  ...buttonConfig,
88
- baseClass: cn(buttonConfig.baseClass, ' flex h-8 w-8 p-0 data-[state=open]:bg-muted'),
91
+ baseClass: [buttonConfig.baseClass, 'flex h-8 w-8 p-0 data-[state=open]:bg-muted'],
89
92
  defaultVariants: {
90
93
  variant: 'ghost',
91
94
  },
@@ -96,14 +99,13 @@ export const DropDownTriggerButton = uic('button', {
96
99
  </>,
97
100
  })
98
101
 
99
- export const DefaultDropdown = ({ children }: {
100
- children: ReactNode
101
- }) => (<DropdownMenu>
102
- <DropdownMenuTrigger asChild>
103
- <DropDownTriggerButton />
104
- </DropdownMenuTrigger>
105
- <DropdownMenuContent className="w-[160px]">
106
- {children}
107
- </DropdownMenuContent>
108
- </DropdownMenu>
102
+ export const DefaultDropdown = ({ children }: { children: ReactNode }) => (
103
+ <DropdownMenu>
104
+ <DropdownMenuTrigger asChild>
105
+ <DropDownTriggerButton />
106
+ </DropdownMenuTrigger>
107
+ <DropdownMenuContent className="w-[160px]">
108
+ {children}
109
+ </DropdownMenuContent>
110
+ </DropdownMenu>
109
111
  )
@@ -1,102 +1,130 @@
1
- import type { ReactNode } from 'react'
2
- import { uic } from '../utils'
3
- import { type RoleCondition, useProjectUserRoles, Link, type RoutingLinkTarget } from '@contember/interface'
1
+ import {
2
+ Link,
3
+ type RoleCondition,
4
+ type RoutingLinkTarget,
5
+ useCurrentRequest,
6
+ useProjectUserRoles,
7
+ } from '@contember/interface'
4
8
  import { createContext } from '@contember/react-utils'
9
+ import { VariantProps } from 'class-variance-authority'
10
+ import { ChevronRight } from 'lucide-react'
11
+ import { Children, isValidElement, ReactNode, useMemo } from 'react'
12
+ import { Collapsible, CollapsibleContent, CollapsibleTrigger } from './collapsible'
13
+ import { SidebarMenu, SidebarMenuButton, sidebarMenuButtonVariants, SidebarMenuItem, SidebarMenuSub } from './sidebar'
5
14
 
6
- export const MenuItemUI = uic('a', {
7
- baseClass: 'flex justify-start py-2.5 px-2.5 w-full gap-2 rounded text-sm items-center transition-all duration-200',
8
- })
9
-
10
- export const MenuItemIconUI = uic('span', {
11
- baseClass: 'w-4 text-gray-400 inline-flex items-center justify-center',
12
- })
13
-
14
- export const MenuSubMenuUI = uic('div', {
15
- baseClass: 'ml-2',
16
- })
17
-
18
- export type MenuItem = {
15
+ export type MenuItemProps = {
19
16
  icon?: ReactNode
20
17
  label: ReactNode
21
18
  to?: RoutingLinkTarget
22
- /** @deprecated use children instead */
23
- subItems?: MenuItem[]
24
- lvl?: number
25
- role?: RoleCondition
26
19
  children?: ReactNode
27
- }
20
+ role?: RoleCondition
21
+ expandedByDefault?: boolean
22
+ } & VariantProps<typeof sidebarMenuButtonVariants>
28
23
 
29
24
  interface MenuContextValue {
30
25
  level: number
31
26
  }
32
27
 
33
- const [MenuContext, useMenuContext] = createContext<MenuContextValue | null>('MenuContext', null)
34
- export const Menu = ({ children }: {
35
- children?: ReactNode
36
- }) => {
37
- return (
38
- <MenuContext.Provider value={{ level: 0 }}>
39
- <div className={'flex flex-col'}>
40
- {children}
41
- </div>
42
- </MenuContext.Provider>
43
- )
28
+ const [MenuContext, useMenuContext] = createContext<MenuContextValue>('MenuContext', { level: 0 })
29
+
30
+ const useIsActiveMenuItem = (to?: RoutingLinkTarget): boolean => {
31
+ const pageName = useCurrentRequest()?.pageName
32
+
33
+ if (!to || !pageName) {
34
+ return false
35
+ }
36
+
37
+ return typeof to === 'string'
38
+ ? pageName === to
39
+ : 'pageName' in to && pageName === to.pageName
44
40
  }
45
41
 
46
- export interface MenuListProps {
47
- items: MenuItem[]
48
- lvl?: number
42
+ const useHasActiveChild = (children: ReactNode) => {
43
+ const currentRequest = useCurrentRequest()
44
+ const pageName = currentRequest?.pageName
45
+
46
+ return useMemo(() => {
47
+ if (!pageName || !children) return false
48
+
49
+ const isMatchingPage = (to: string | { pageName: string }): boolean => {
50
+ return typeof to === 'string'
51
+ ? pageName === to
52
+ : 'pageName' in to && pageName === to.pageName
53
+ }
54
+
55
+ const checkChild = (child: ReactNode): boolean => {
56
+ if (!isValidElement(child)) return false
57
+
58
+ const { to, children: grandChildren } = child.props
59
+
60
+ if (to && isMatchingPage(to)) return true
61
+
62
+ if (!grandChildren) return false
63
+
64
+ return Children.toArray(grandChildren).some(checkChild)
65
+ }
66
+
67
+ return Children.toArray(children).some(checkChild)
68
+ }, [children, pageName])
49
69
  }
50
70
 
51
- /**
52
- * @deprecated use Menu instead
53
- */
54
- export const MenuList = ({ items, lvl = 0 }: MenuListProps) => {
71
+ export const Menu = ({ children }: { children: ReactNode }) => {
55
72
  return (
56
- <div className={'flex flex-col'}>
57
- {items.map((item, index) => (
58
- <MenuItem key={index} {...item} lvl={lvl} />
59
- ))}
60
- </div>
73
+ <MenuContext.Provider value={{ level: 0 }}>
74
+ <SidebarMenu>
75
+ {children}
76
+ </SidebarMenu>
77
+ </MenuContext.Provider>
61
78
  )
62
79
  }
63
80
 
64
- export const MenuItem = ({ icon, label, to, subItems, lvl, role, children }: MenuItem) => {
81
+ export const MenuItem = ({ icon, label, to, children, role, expandedByDefault, ...menuButtonProps }: MenuItemProps) => {
82
+ const isActive = useIsActiveMenuItem(to)
83
+ const hasActiveChild = useHasActiveChild(children)
65
84
  const projectRoles = useProjectUserRoles()
66
- const menu = useMenuContext()
67
- lvl ??= menu?.level ?? 0
85
+ const { level } = useMenuContext()
86
+
68
87
  if (role && !(typeof role === 'string' ? projectRoles.has(role) : role(projectRoles))) {
69
88
  return null
70
89
  }
71
90
 
72
- return (
73
- <div>
74
- {to ? (
91
+ const hasChildren = Boolean(children)
92
+
93
+ if (!hasChildren && to) {
94
+ return (
95
+ <SidebarMenuItem>
75
96
  <Link to={to}>
76
- <MenuItemUI className={'hover:bg-gray-100 cursor-pointer'}>
77
- <MenuItemIconUI>{icon}</MenuItemIconUI>
78
- <span className={lvl === 0 ? 'font-medium' : ''}>{label}</span>
79
- </MenuItemUI>
97
+ <SidebarMenuButton isActive={isActive} tooltip={label} {...menuButtonProps}>
98
+ {icon}
99
+ <span>{label}</span>
100
+ </SidebarMenuButton>
80
101
  </Link>
81
- ) : (
82
- <MenuItemUI>
83
- <MenuItemIconUI>{icon}</MenuItemIconUI>
84
- <span className={lvl === 0 ? 'font-medium' : ''}>{label}</span>
85
- </MenuItemUI>
86
- )}
87
- {subItems && (
88
- <MenuSubMenuUI>
89
- <MenuList items={subItems} lvl={lvl + 1}/>
90
- </MenuSubMenuUI>
91
- )}
92
-
93
- {children && (
94
- <MenuSubMenuUI>
95
- <MenuContext.Provider value={{ level: lvl + 1 }}>
96
- {children}
97
- </MenuContext.Provider>
98
- </MenuSubMenuUI>
99
- )}
100
- </div>
102
+ </SidebarMenuItem>
103
+ )
104
+ }
105
+
106
+ return (
107
+ <Collapsible defaultOpen={expandedByDefault || hasActiveChild} className="[&[data-state=open]>li>button>.menu-icon]:rotate-90">
108
+ <SidebarMenuItem>
109
+ <CollapsibleTrigger asChild>
110
+ <SidebarMenuButton isActive={isActive} tooltip={label} {...menuButtonProps}>
111
+ {icon}
112
+ <span>{label}</span>
113
+ {hasChildren && (
114
+ <ChevronRight className="ml-auto transition-transform duration-200" />
115
+ )}
116
+ </SidebarMenuButton>
117
+ </CollapsibleTrigger>
118
+ {hasChildren && (
119
+ <CollapsibleContent className="collapsible-animate">
120
+ <SidebarMenuSub>
121
+ <MenuContext.Provider value={{ level: level + 1 }}>
122
+ {children}
123
+ </MenuContext.Provider>
124
+ </SidebarMenuSub>
125
+ </CollapsibleContent>
126
+ )}
127
+ </SidebarMenuItem>
128
+ </Collapsible>
101
129
  )
102
130
  }
@@ -1,9 +1,9 @@
1
1
  import * as React from 'react'
2
2
  import * as PopoverPrimitive from '@radix-ui/react-popover'
3
- import { cn } from '../utils'
3
+ import { cn, uic } from '../utils'
4
4
  import { createComponentOpenHooks } from '../utils/createComponentOpenHooks'
5
5
 
6
- const { Component: Popover, useOpen: usePopoverOpenstate } = createComponentOpenHooks(PopoverPrimitive.Root)
6
+ const { Component: Popover, useOpen: usePopoverOpenState } = createComponentOpenHooks(PopoverPrimitive.Root)
7
7
 
8
8
  const PopoverTrigger = PopoverPrimitive.Trigger
9
9
 
@@ -1,25 +1,23 @@
1
- import * as React from 'react'
2
1
  import * as ProgressPrimitive from '@radix-ui/react-progress'
3
- import { cn } from '../utils/cn'
2
+ import * as React from 'react'
3
+ import { uic } from '../utils'
4
+
5
+ const ProgressRoot = uic(ProgressPrimitive.Root, {
6
+ baseClass: 'relative h-1.5 w-full overflow-hidden rounded-full bg-primary/20',
7
+ displayName: ProgressPrimitive.Root.displayName,
8
+ })
4
9
 
5
- const Progress = React.forwardRef<
10
+ const ProgressIndicator = uic(ProgressPrimitive.Indicator, {
11
+ baseClass: 'h-full w-full flex-1 transition-all bg-gradient-to-b from-blue-500 to-blue-400',
12
+ displayName: ProgressPrimitive.Indicator.displayName,
13
+ })
14
+
15
+ export const Progress = React.forwardRef<
6
16
  React.ElementRef<typeof ProgressPrimitive.Root>,
7
17
  React.ComponentPropsWithoutRef<typeof ProgressPrimitive.Root>
8
18
  >(({ className, value, ...props }, ref) => (
9
- <ProgressPrimitive.Root
10
- ref={ref}
11
- className={cn(
12
- 'relative h-1.5 w-full overflow-hidden rounded-full bg-primary/20',
13
- className,
14
- )}
15
- {...props}
16
- >
17
- <ProgressPrimitive.Indicator
18
- className="h-full w-full flex-1 transition-all bg-gradient-to-b from-blue-500 to-blue-400"
19
- style={{ transform: `translateX(-${100 - (value || 0)}%)` }}
20
- />
21
- </ProgressPrimitive.Root>
19
+ <ProgressRoot ref={ref} {...props}>
20
+ <ProgressIndicator style={{ transform: `translateX(-${100 - (value || 0)}%)` }} />
21
+ </ProgressRoot>
22
22
  ))
23
23
  Progress.displayName = ProgressPrimitive.Root.displayName
24
-
25
- export { Progress }