Skip to content

Commit

Permalink
feat: nuxt auto-import selected components (#492)
Browse files Browse the repository at this point in the history
* feat: select components to auto-import

* fix: uncapitalize components keys
  • Loading branch information
enkot authored Nov 3, 2023
1 parent 9526356 commit 0bc37e7
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 36 deletions.
19 changes: 17 additions & 2 deletions packages/plugins/src/nuxt/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { addComponent, defineNuxtModule } from '@nuxt/kit'

import type { } from '@nuxt/schema' // workaround for TS bug with "phantom" deps
import * as components from 'radix-vue'
import { components as allComponents } from '../../../radix-vue/constant/components'

export interface ModuleOptions {
components: Partial<Record<keyof typeof allComponents, boolean>> | boolean
prefix: string
}

Expand All @@ -17,9 +18,23 @@ export default defineNuxtModule<ModuleOptions>({
},
defaults: {
prefix: '',
components: true,
},
setup(options) {
for (const component of Object.keys(components)) {
function getComponents() {
if (typeof options.components === 'object') {
return Object.entries(allComponents)
.filter(([name]) => (options.components as Record<string, boolean>)[name])
.flatMap(([_, components]) => components)
}

if (options.components)
return Object.values(allComponents).flat()

return []
}

for (const component of getComponents()) {
addComponent({
name: `${options.prefix}${component}`,
export: component,
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/src/resolver/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function (options: ResolverOptions = {}): ComponentResolver {
return {
type: 'component',
resolve: (name: string) => {
if (name.startsWith(prefix)) {
if (name.toLowerCase().startsWith(prefix.toLowerCase())) {
const componentName = name.substring(prefix.length)
if (Object.values(components).flat().includes(componentName)) {
return {
Expand Down
66 changes: 33 additions & 33 deletions packages/radix-vue/constant/components.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
export const components = {
Accordion: [
accordion: [
'AccordionContent',
'AccordionHeader',
'AccordionItem',
'AccordionRoot',
'AccordionTrigger',
],

Checkbox: [
checkbox: [
'CheckboxRoot',
'CheckboxIndicator',
],

Collapsible: [
collapsible: [
'CollapsibleRoot',
'CollapsibleTrigger',
'CollapsibleContent',
],

Combobox: [
combobox: [
'ComboboxRoot',
'ComboboxInput',
'ComboboxAnchor',
Expand All @@ -36,54 +36,54 @@ export const components = {
'ComboboxPortal',
],

Switch: [
switch: [
'SwitchRoot',
'SwitchThumb',
],

Separator: [
separator: [
'Separator',
],

Label: [
label: [
'Label',
],

Toggle: [
toggle: [
'Toggle',
],

ToggleGroup: [
toggleGroup: [
'ToggleGroupRoot',
'ToggleGroupItem',
],

AspectRatio: [
aspectRatio: [
'AspectRatio',
],

Tabs: [
tabs: [
'TabsRoot',
'TabsList',
'TabsContent',
'TabsTrigger',
],

Slider: [
slider: [
'SliderRoot',
'SliderThumb',
'SliderTrack',
'SliderRange',
],

RadioGroup: [
radioGroup: [
'RadioGroupRoot',
'RadioGroupItem',
'RadioGroupIndicator',

],

Pagination: [
pagination: [
'PaginationRoot',
'PaginationEllipsis',
'PaginationFirst',
Expand All @@ -94,12 +94,12 @@ export const components = {
'PaginationPrev',
],

Progress: [
progress: [
'ProgressRoot',
'ProgressIndicator',
],

Dialog: [
dialog: [
'DialogRoot',
'DialogTrigger',
'DialogPortal',
Expand All @@ -110,7 +110,7 @@ export const components = {
'DialogDescription',
],

AlertDialog: [
alertDialog: [
'AlertDialogRoot',
'AlertDialogTrigger',
'AlertDialogPortal',
Expand All @@ -122,7 +122,7 @@ export const components = {
'AlertDialogAction',
],

Toast: [
toast: [
'ToastProvider',
'ToastRoot',
'ToastAction',
Expand All @@ -132,7 +132,7 @@ export const components = {
'ToastDescription',
],

Toolbar: [
toolbar: [
'ToolbarRoot',
'ToolbarButton',
'ToolbarLink',
Expand All @@ -141,13 +141,13 @@ export const components = {
'ToolbarSeparator',
],

Avatar: [
avatar: [
'AvatarRoot',
'AvatarImage',
'AvatarFallback',
],

Tooltip: [
tooltip: [
'TooltipRoot',
'TooltipTrigger',
'TooltipContent',
Expand All @@ -156,15 +156,15 @@ export const components = {
'TooltipProvider',
],

HoverCard: [
hoverCard: [
'HoverCardRoot',
'HoverCardTrigger',
'HoverCardPortal',
'HoverCardContent',
'HoverCardArrow',
],

Popover: [
popover: [
'PopoverRoot',
'PopoverTrigger',
'PopoverPortal',
Expand All @@ -174,7 +174,7 @@ export const components = {
'PopoverAnchor',
],

DropdownMenu: [
dropdownMenu: [
'DropdownMenuRoot',
'DropdownMenuTrigger',
'DropdownMenuPortal',
Expand All @@ -193,7 +193,7 @@ export const components = {
'DropdownMenuSubTrigger',
],

ContextMenu: [
contextMenu: [
'ContextMenuRoot',
'ContextMenuTrigger',
'ContextMenuPortal',
Expand All @@ -212,7 +212,7 @@ export const components = {
'ContextMenuSubTrigger',
],

Select: [
select: [
'SelectRoot',
'SelectTrigger',
'SelectPortal',
Expand All @@ -231,7 +231,7 @@ export const components = {
'SelectIcon',
],

Menubar: [
menubar: [
'MenubarRoot',
'MenubarTrigger',
'MenubarPortal',
Expand All @@ -251,7 +251,7 @@ export const components = {
'MenubarMenu',
],

NavigationMenu: [
navigationMenu: [
'NavigationMenuContent',
'NavigationMenuIndicator',
'NavigationMenuItem',
Expand All @@ -263,30 +263,30 @@ export const components = {
'NavigationMenuViewport',
],

ScrollArea: [
scrollArea: [
'ScrollAreaRoot',
'ScrollAreaViewport',
'ScrollAreaScrollbar',
'ScrollAreaThumb',
'ScrollAreaCorner',
],

ConfigProvider: [
configProvider: [
'ConfigProvider',
],

Primitive: [
primitive: [
'Primitive',
'Slot',
],

VisuallyHidden: [
visuallyHidden: [
'VisuallyHidden',
],
}

export const utilities = {
Utilities: [
utilities: [
'useId',
'useEmitAsProps',
'useForwardProps',
Expand Down
3 changes: 3 additions & 0 deletions playground/nuxt/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ export default defineNuxtConfig({
path: '../../docs/components/demo',
},
],
radix: {
components: true,
},
})

0 comments on commit 0bc37e7

Please sign in to comment.