File size: 11,762 Bytes
0bfe2e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
'use client';

import * as DropdownMenuPrimitive from '@radix-ui/react-dropdown-menu';
import { cva } from 'class-variance-authority';
import * as React from 'react';
import { cn, ComponentAnatomy, defineStyleAnatomy } from '../core/styling';

/* -------------------------------------------------------------------------------------------------
 * Anatomy
 * -----------------------------------------------------------------------------------------------*/

export const DropdownMenuAnatomy = defineStyleAnatomy({
  subTrigger: cva([
    'UI-DropdownMenu__subTrigger',
    'focus:bg-[--subtle] data-[state=open]:bg-[--subtle]',
  ]),
  subContent: cva([
    'UI-DropdownMenu__subContent',
    'z-50 min-w-[12rem] overflow-hidden rounded-[--radius] border bg-[--background] p-2 text-[--foreground] shadow-sm',
    'data-[state=open]:animate-in data-[state=closed]:animate-out',
    'data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0',
    'data-[state=closed]:zoom-out-100 data-[state=open]:zoom-in-95',
    'data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2',
    'data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2',
  ]),
  root: cva([
    'UI-DropdownMenu__root',
    'z-50 min-w-[15rem] overflow-hidden rounded-[--radius] border bg-[--background] p-2 text-[--foreground] shadow-sm',
    'data-[state=open]:animate-in data-[state=closed]:animate-out',
    'data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0',
    'data-[state=closed]:zoom-out-100 data-[state=open]:zoom-in-95',
    'data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2',
    'data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2',
  ]),
  item: cva([
    'UI-DropdownMenu__item',
    'relative flex cursor-default select-none items-center rounded-[--radius] cursor-pointer px-2 py-2 text-sm outline-none transition-colors',
    'focus:bg-[--subtle] data-[disabled]:pointer-events-none',
    'data-[disabled]:opacity-50',
    '[&>svg]:mr-2 [&>svg]:text-lg',
  ]),
  group: cva(['UI-DropdownMenu__group']),
  label: cva([
    'UI-DropdownMenu__label',
    'px-2 py-1.5 text-sm font-semibold text-[--muted]',
  ]),
  separator: cva([
    'UI-DropdownMenu__separator',
    '-mx-1 my-2 h-px bg-[--border]',
  ]),
  shortcut: cva([
    'UI-DropdownMenu__shortcut',
    'ml-auto text-xs tracking-widest opacity-60',
  ]),
});

/* -------------------------------------------------------------------------------------------------
 * DropdownMenu
 * -----------------------------------------------------------------------------------------------*/

const __DropdownMenuAnatomyContext = React.createContext<
  ComponentAnatomy<typeof DropdownMenuAnatomy>
>({});

export type DropdownMenuProps = ComponentAnatomy<typeof DropdownMenuAnatomy> &
  Pick<
    React.ComponentPropsWithoutRef<typeof DropdownMenuPrimitive.Root>,
    'defaultOpen' | 'open' | 'onOpenChange' | 'dir'
  > &
  React.ComponentPropsWithoutRef<typeof DropdownMenuPrimitive.Content> & {
    /**
     * Interaction with outside elements will be enabled and other elements will be visible to screen readers.
     */
    allowOutsideInteraction?: boolean;
    /**
     * The trigger element that is always visible and is used to open the menu.
     */
    trigger?: React.ReactNode;
  };

export const DropdownMenu = React.forwardRef<HTMLDivElement, DropdownMenuProps>(
  (props, ref) => {
    const {
      children,
      trigger,
      // Root
      defaultOpen,
      open,
      onOpenChange,
      dir,
      allowOutsideInteraction,
      // Content
      sideOffset = 4,
      className,
      subContentClass,
      subTriggerClass,
      shortcutClass,
      itemClass,
      labelClass,
      separatorClass,
      groupClass,
      ...rest
    } = props;

    return (
      <__DropdownMenuAnatomyContext.Provider
        value={{
          subContentClass,
          subTriggerClass,
          shortcutClass,
          itemClass,
          labelClass,
          separatorClass,
          groupClass,
        }}
      >
        <DropdownMenuPrimitive.Root
          defaultOpen={defaultOpen}
          open={open}
          onOpenChange={onOpenChange}
          dir={dir}
          modal={!allowOutsideInteraction}
          {...rest}
        >
          <DropdownMenuPrimitive.Trigger asChild>
            {trigger}
          </DropdownMenuPrimitive.Trigger>

          <DropdownMenuPrimitive.Portal>
            <DropdownMenuPrimitive.Content
              ref={ref}
              sideOffset={sideOffset}
              className={cn(DropdownMenuAnatomy.root(), className)}
              {...rest}
            >
              {children}
            </DropdownMenuPrimitive.Content>
          </DropdownMenuPrimitive.Portal>
        </DropdownMenuPrimitive.Root>
      </__DropdownMenuAnatomyContext.Provider>
    );
  }
);

DropdownMenu.displayName = 'DropdownMenu';

/* -------------------------------------------------------------------------------------------------
 * DropdownMenuGroup
 * -----------------------------------------------------------------------------------------------*/

export type DropdownMenuGroupProps = React.ComponentPropsWithoutRef<
  typeof DropdownMenuPrimitive.Group
>;

export const DropdownMenuGroup = React.forwardRef<
  HTMLDivElement,
  DropdownMenuGroupProps
>((props, ref) => {
  const { className, ...rest } = props;

  const { groupClass } = React.useContext(__DropdownMenuAnatomyContext);

  return (
    <DropdownMenuPrimitive.Group
      ref={ref}
      className={cn(DropdownMenuAnatomy.group(), groupClass, className)}
      {...rest}
    />
  );
});

DropdownMenuGroup.displayName = 'DropdownMenuGroup';

/* -------------------------------------------------------------------------------------------------
 * DropdownMenuSub
 * -----------------------------------------------------------------------------------------------*/

export type DropdownMenuSubProps = Pick<
  ComponentAnatomy<typeof DropdownMenuAnatomy>,
  'subTriggerClass'
> &
  Pick<
    React.ComponentPropsWithoutRef<typeof DropdownMenuPrimitive.Sub>,
    'defaultOpen' | 'open' | 'onOpenChange'
  > &
  React.ComponentPropsWithoutRef<typeof DropdownMenuPrimitive.SubContent> & {
    /**
     * The content of the default trigger element that will open the sub menu.
     *
     * By default, the trigger will be an item with a right chevron icon.
     */
    triggerContent?: React.ReactNode;
    /**
     * Props to pass to the default trigger element that will open the sub menu.
     */
    triggerProps?: React.ComponentPropsWithoutRef<
      typeof DropdownMenuPrimitive.SubTrigger
    >;
    triggerInset?: boolean;
  };

export const DropdownMenuSub = React.forwardRef<
  HTMLDivElement,
  DropdownMenuSubProps
>((props, ref) => {
  const {
    children,
    triggerContent,
    triggerProps,
    triggerInset,
    // Sub
    defaultOpen,
    open,
    onOpenChange,
    // SubContent
    sideOffset = 8,
    className,
    subTriggerClass,
    ...rest
  } = props;

  const { subTriggerClass: _subTriggerClass, subContentClass } =
    React.useContext(__DropdownMenuAnatomyContext);

  return (
    <DropdownMenuPrimitive.Sub {...rest}>
      <DropdownMenuPrimitive.SubTrigger
        className={cn(
          DropdownMenuAnatomy.item(),
          DropdownMenuAnatomy.subTrigger(),
          triggerInset && 'pl-8',
          _subTriggerClass,
          subTriggerClass,
          className
        )}
        {...triggerProps}
      >
        {triggerContent}
        <svg
          xmlns="http://www.w3.org/2000/svg"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          strokeWidth="2"
          strokeLinecap="round"
          strokeLinejoin="round"
          className={cn(DropdownMenuAnatomy.shortcut(), 'w-4 h-4 ml-auto')}
        >
          <path d="m9 18 6-6-6-6" />
        </svg>
      </DropdownMenuPrimitive.SubTrigger>

      <DropdownMenuPrimitive.Portal>
        <DropdownMenuPrimitive.SubContent
          ref={ref}
          sideOffset={sideOffset}
          className={cn(
            DropdownMenuAnatomy.subContent(),
            subContentClass,
            className
          )}
          {...rest}
        >
          {children}
        </DropdownMenuPrimitive.SubContent>
      </DropdownMenuPrimitive.Portal>
    </DropdownMenuPrimitive.Sub>
  );
});

DropdownMenuSub.displayName = 'DropdownMenuSub';

/* -------------------------------------------------------------------------------------------------
 * DropdownMenuItem
 * -----------------------------------------------------------------------------------------------*/

export type DropdownMenuItemProps = React.ComponentPropsWithoutRef<
  typeof DropdownMenuPrimitive.Item
> & {
  inset?: boolean;
};

export const DropdownMenuItem = React.forwardRef<
  HTMLDivElement,
  DropdownMenuItemProps
>((props, ref) => {
  const { className, inset, ...rest } = props;

  const { itemClass } = React.useContext(__DropdownMenuAnatomyContext);

  return (
    <DropdownMenuPrimitive.Item
      ref={ref}
      className={cn(
        DropdownMenuAnatomy.item(),
        inset && 'pl-8',
        itemClass,
        className
      )}
      {...rest}
    />
  );
});
DropdownMenuItem.displayName = 'DropdownMenuItem';

/* -------------------------------------------------------------------------------------------------
 * DropdownMenuLabel
 * -----------------------------------------------------------------------------------------------*/

export type DropdownMenuLabelProps = React.ComponentPropsWithoutRef<
  typeof DropdownMenuPrimitive.Label
> & {
  inset?: boolean;
};

export const DropdownMenuLabel = React.forwardRef<
  HTMLDivElement,
  DropdownMenuLabelProps
>((props, ref) => {
  const { className, inset, ...rest } = props;

  const { labelClass } = React.useContext(__DropdownMenuAnatomyContext);

  return (
    <DropdownMenuPrimitive.Label
      ref={ref}
      className={cn(
        DropdownMenuAnatomy.label(),
        inset && 'pl-8',
        labelClass,
        className
      )}
      {...rest}
    />
  );
});

DropdownMenuLabel.displayName = 'DropdownMenuLabel';

/* -------------------------------------------------------------------------------------------------
 * DropdownMenuSeparator
 * -----------------------------------------------------------------------------------------------*/

export type DropdownMenuSeparatorProps = React.ComponentPropsWithoutRef<
  typeof DropdownMenuPrimitive.Separator
>;

export const DropdownMenuSeparator = React.forwardRef<
  HTMLDivElement,
  DropdownMenuSeparatorProps
>((props, ref) => {
  const { className, ...rest } = props;

  const { separatorClass } = React.useContext(__DropdownMenuAnatomyContext);

  return (
    <DropdownMenuPrimitive.Separator
      ref={ref}
      className={cn(DropdownMenuAnatomy.separator(), separatorClass, className)}
      {...rest}
    />
  );
});

DropdownMenuSeparator.displayName = 'DropdownMenuSeparator';

/* -------------------------------------------------------------------------------------------------
 * DropdownMenuShortcut
 * -----------------------------------------------------------------------------------------------*/

export type DropdownMenuShortcutProps = React.HTMLAttributes<HTMLSpanElement>;

export const DropdownMenuShortcut = React.forwardRef<
  HTMLSpanElement,
  DropdownMenuShortcutProps
>((props, ref) => {
  const { className, ...rest } = props;

  const { shortcutClass } = React.useContext(__DropdownMenuAnatomyContext);

  return (
    <span
      ref={ref}
      className={cn(DropdownMenuAnatomy.shortcut(), shortcutClass, className)}
      {...rest}
    />
  );
});

DropdownMenuShortcut.displayName = 'DropdownMenuShortcut';