All Projects → nuxt → Components

nuxt / Components

Licence: mit
Scan and auto import components for Nuxt.js 2.13+

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Components

Bootstrap Vue
BootstrapVue provides one of the most comprehensive implementations of Bootstrap v4 for Vue.js. With extensive and automated WAI-ARIA accessibility markup.
Stars: ✭ 13,603 (+1930.3%)
Mutual labels:  nuxt, components
Shards React
⚛️A beautiful and modern React design system.
Stars: ✭ 639 (-4.63%)
Mutual labels:  components
Nuxt Type
Small demo showing custom page animations with a fake typography site
Stars: ✭ 544 (-18.81%)
Mutual labels:  nuxt
Design System
Priceline.com Design System
Stars: ✭ 604 (-9.85%)
Mutual labels:  components
Helenos
A portable microkernel-based multiserver operating system written from scratch.
Stars: ✭ 553 (-17.46%)
Mutual labels:  components
React Fine Uploader
Easily integrate Fine Uploader or Fine Uploader S3 into a React app. Drop-in high-level components for a turn-key UI. Use small focused components to build a more custom UI.
Stars: ✭ 628 (-6.27%)
Mutual labels:  components
Typescript Template
Typescript starter with Nuxt.js
Stars: ✭ 539 (-19.55%)
Mutual labels:  nuxt
React Fontawesome
A React Font Awesome component.
Stars: ✭ 662 (-1.19%)
Mutual labels:  components
Frend.co
Frend — A collection of accessible, modern front-end components.
Stars: ✭ 640 (-4.48%)
Mutual labels:  components
Awes Io
Awes.io // boilerplate based on Vue, Nuxt, TailwindCSS plus Laravel as a backend. 🤟
Stars: ✭ 599 (-10.6%)
Mutual labels:  nuxt
Starter Template
DEPRECATED: use create-nuxt-app instead
Stars: ✭ 598 (-10.75%)
Mutual labels:  nuxt
Google Analytics Module
Google Analytics Module
Stars: ✭ 556 (-17.01%)
Mutual labels:  nuxt
Validation
The most awesome validation engine ever created for PHP
Stars: ✭ 5,484 (+718.51%)
Mutual labels:  components
Etch
Builds components using a simple and explicit API around virtual-dom
Stars: ✭ 545 (-18.66%)
Mutual labels:  components
Wemake Vue Template
Bleeding edge vue template focused on code quality and developer happiness.
Stars: ✭ 645 (-3.73%)
Mutual labels:  nuxt
Sitemap Module
Sitemap Module for Nuxt
Stars: ✭ 539 (-19.55%)
Mutual labels:  nuxt
Css Components
☕️ A set of common UI Components using the power of CSS and without Javascript.
Stars: ✭ 592 (-11.64%)
Mutual labels:  components
Ngx Bootstrap
Fast and reliable Bootstrap widgets in Angular (supports Ivy engine)
Stars: ✭ 5,343 (+697.46%)
Mutual labels:  components
Example Auth0
A simple example that shows how to use Nuxt.js with Auth0.
Stars: ✭ 664 (-0.9%)
Mutual labels:  nuxt
Fast
The adaptive interface system for modern web experiences.
Stars: ✭ 6,532 (+874.93%)
Mutual labels:  components

@nuxt/components

@nuxt/components

npm version npm downloads Github Actions CI Codecov License

Module to scan and auto import components for Nuxt 2.13+

Table of Contents

Features

  • Automatically scan components/ directory
  • No need to manually import components anymore
  • Multiple paths with customizable prefixes and lookup/ignore patterns
  • Lazy loading (Async components)
  • Production code-splitting optimization (loader)
  • Hot reloading
  • Module integration (library authors)
  • Fully tested

Usage

Set the components option in nuxt.config:

export default {
  components: true
}

Note: If using nuxt 2.10...2.13, you have to also manually install and add @nuxt/components to buildModules inside nuxt.config.

Create your components:

| components/
---| ComponentFoo.vue
---| ComponentBar.vue

Use them whenever you want, they will be auto imported in .vue files :

<template>
  <ComponentFoo />
  <component-bar />
</template>

No need anymore to manually import them in the script section!

See live demo or video example.

Lazy Imports

Nuxt by default does code-slitting per page and components. But sometimes we also need to lazy load them:

  • Component size is rather big (or has big dependencies imported) like a text-editor
  • Component is rendered conditionally with v-if or being in a modal

In order to lazy load a component, all we need to do is to add Lazy prefix to component name.

You now can easily import a component on-demand:

<template>
  <LazyComponentFoo v-if="foo" />
  <button @click="loadFoo">
    Load Foo
  </button>
</template>

<script>
export default {
  data () {
    return {
      foo: null
    }
  },
  methods: {
    async loadFoo () {
      this.foo = await this.$axios.$get('foo')
    }
  }
}
</script>

Nested Components

If you have components in nested directories:

| components/
---| my/
---| form/
------| TextArea.vue

The component name will contain its path:

<MyFormTextArea />

For clarity, it is recommended that component file name matches its name. You can also use MyFormTextArea.vue as name with same directory structure.

If for any reason different prefix is desired, we can add specific directory with the prefix option: (See directories section)

components: [
  '~/components/',
  { path: '~/components/foo/', prefix: 'foo' }
]

Overwriting Components

It is possible to have a way to overwrite components using the level option. This is very useful for modules and theme authors.

Considering this structure:

| node_modules/
---| my-theme/
------| components/
---------| Header.vue
| components/
---| Header.vue

Then defining in the nuxt.config:

components: [
  '~/components', // default level is 0
  { path: 'node_modules/my-theme/components', level: 1 }
]

Our components/Header.vue will overwrite our theme component since the lowest level overwrites.

Directories

By setting components: true, default ~/components directory will be included. However you can customize module behaviour by providing directories to scan:

export default {
  components: [
    '~/components', // shortcut to { path: '~/components' }
    { path: '~/components/awesome/', prefix: 'awesome' }
  ],
}

Each item can be either string or object. String is shortcut to { path }.

Note: Don't worry about ordering or overlapping directories! Components module will take care of it. Each file will be only matched once with longest path.

Directory Properties

path

  • Required
  • Type: String

Path (absolute or relative) to the directory containing your components.

You can use Nuxt aliases (~ or @) to refer to directories inside project or directly use a npm package path similar to require.

extensions

  • Type: Array<string>
  • Default:
    • Extensions supported by Nuxt builder (builder.supportedExtensions)
    • Default supported extensions ['vue', 'js'] or ['vue', 'js', 'ts', 'tsx'] depending on your environment

Example: Support multi-file component structure

If you prefer to split your SFCs into .js, .vue and .css, you can only enable .vue files to be scanned:

| components
---| componentC
------| componentC.vue
------| componentC.js
------| componentC.scss
// nuxt.config.js
export default {
  components: [
    { path: '~/components', extensions: ['vue'] }
  ]
}

pattern

  • Type: string (glob pattern)
  • Default: **/*.${extensions.join(',')}

Accept Pattern that will be run against specified path.

ignore

Ignore patterns that will be run against specified path.

prefix

  • Type: String
  • Default: '' (no prefix)

Prefix all matched components.

Example below adds awesome-/Awesome prefix to the name of components in awesome/ directory.

// nuxt.config.js
export default {
  components: [
      '~/components',
      { path: '~/components/awesome/', prefix: 'awesome' }
  ]
}
components/
  awesome/
    Button.vue
  Button.vue
<template>
  <div>
    <AwesomeButton>Click on me 🤘</AwesomeButton>
    <Button>Click on me</Button>
  </div>
</template>

pathPrefix

  • Type: Boolean
  • Default: true

Prefix component name by it's path

watch

  • Type: Boolean
  • Default: true

Watch specified path for changes, including file additions and file deletions.

transpile

  • Type: Boolean
  • Default: 'auto'

Transpile specified path using build.transpile, by default ('auto') it will set transpile: true if node_modules/ is in path.

level

  • Type: Number
  • Default: 0

Level are use to define a hint when overwriting the components which have the same name in two different directories, this is useful for theming.

export default {
  components: [
    '~/components', // default level is 0
   { path: 'my-theme/components', level: 1 }
  ]
}

Components having the same name in ~/components will overwrite the one in my-theme/components, learn more in Overwriting Components. The lowest value will overwrite.

Migration guide

v1 to v2

Starting with [email protected], Nuxt uses @nuxt/components v2:

  • All components are globally available so you can move components/global/ to components/ and global: true is not required anymore
  • Full path inside components is used to prefix component names. If you were structing your components in multiple directories, should either add prefix or register in components section of nuxt.config or use new pathPrefix option.

Example:

components
├── atoms
│   └── icons
├── molecules
│   └── illustrations
├── organisms
│   └── ads
└── templates
    ├── blog
    └── home
// nuxt.config.js
export default {
  components: [
    '~/components/templates',
    '~/components/atoms',
    '~/components/molecules',
    '~/components/organisms',
  ]
}

Library Authors

Making Vue Component libraries with automatic tree-shaking and component registration is now damn easy ✨

This module expose a hook named components:dirs so you can easily extend the directory list without updating user configuration in your Nuxt module.

Imagine a directory structure like this:

| node_modules/
---| awesome-ui/
------| components/
---------| Alert.vue
---------| Button.vue
------| nuxt.js
| pages/
---| index.vue
| nuxt.config.js

Then in awesome-ui/nuxt.js you can use the components:dir hook:

import { join } from 'path'

export default function () {
  this.nuxt.hook('components:dirs', (dirs) => {
    // Add ./components dir to the list
    dirs.push({
      path: join(__dirname, 'components'),
      prefix: 'awesome'
    })
  })
}

That's it! Now in your project, you can import your ui library as a Nuxt module in your nuxt.config.js:

export default {
  buildModules: [
    '@nuxt/components',
    'awesome-ui/nuxt'
  ]
}

And directly use the module components (prefixed with awesome-), our pages/index.vue:

<template>
  <div>
    My <AwesomeButton>UI button</AwesomeButton>!
    <awesome-alert>Here's an alert!</awesome-alert>
  </div>
</template>

It will automatically import the components only if used and also support HMR when updating your components in node_modules/awesome-ui/components/.

Next: publish your awesome-ui module to npm and share it with the other Nuxters ✨

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].