npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-primitives

v1.2.0

Published

A clever component factory that makes templates even more legible

Downloads

17

Readme

Vue Primitives

A clever component factory that makes templates even more legible

Before:

<div class="alert-box" role="alert">
  <div class="alert-title bg-red text-white font-bold rounded-t px-4 py-2">Danger</div>
  <div class="alert-body border border-t-0 border-red-light rounded-b bg-red-lightest px-4 py-3 text-red-dark">
    <p>Something not ideal might be happening.</p>
  </div>
</div>

After:

<alert-box>
  <alert-title>Danger</alert-title>
  <alert-body>
    <p>Something not ideal might be happening.</p>
  </alert-body>
</alert-box>

Getting Started

npm install --save vue-primitives

Register locally with primitives():

import { primitives } from 'vue-primitives'

export default {
  components: {
    ...primitives({
      Message: { tag: 'span' },
      Card: { tag: 'div', classes: 'bg-red' },
    }),
  }
}

Register globally with registerPrimitives:

import { primitives } from 'vue-primitives'

export default {
  components: {
    ...primitives({
      Message: { tag: 'span' },
      Card: { tag: 'div', classes: 'bg-red' },
    }),
  }
}

Features

Bake in additional classes:

createPrimitive({ tag: 'div', name: 'Card', classes: 'p-1 md:p-3'})
// Before: <div class="card pa-sm-1 pa-md-3">
// After:  <card>

[Advanced] Conditionally apply classes:

createPrimitive({ 
  tag: 'div', 
  name: 'Frame',
  conditionals: [
    ({store}) => store.getters.darkMode ? 'white-text white-border' : 'black-text black-border' 
  ]
})

Conditionals should return Strings. The data passed into each conditional is an object {context, store, newTag, options, classes} where context is the render context of a Vue render function, store is a helper to access the Vuex store if present, newTag is the target name for the new primitive, options is the Object passed into createPrimitive(), and classes is a list of all classes to be applied to the primitive so far.

[Advanced] Access the render context:

The data key will be merged with context.data of the functional component render context. For full API see here.

createPrimitive({ 
  tag: 'div', 
  name: 'CloseBtn',
  data: {
    domProps: {
      innerHTML: ''
    }
  }
})

Usage

Unknown attrs without values become classes

This:

<message text-2xl id="1">

Results in:

<span class="message text-2xl" id="1">
  • class and :class work as expected, as do style and :style. (This is not true by default for functional components - it must be constructed from scratch)

  • Common attrs like id will fall through, but data- attrs will not. You can use the Functional Context Data interface (see above).

  • All other attrs become classes. Ex. <box big> will be a box with the additional class big. This is very useful with helper classes. You can also v-bind them to a boolean to toggle them as expected.

Dev Setup

The GitHub repo has a working example you can pull down.

  • Clone from GitHub repo (https://github.com/sirberus/vue-primitives)

  • In /example/, open a terminal, npm install, and npm run serve

ToDos:

  • Make attribute-to-class mapping toggleable.

  • Prop aliasing system. Supports a few use cases:

    • Prettify ugly helper classes, eg 'red' instead of 'text--red'

    • Shortening, eg 'row' instead of 'display-flex flex-direction-row'

    • Usable as baked-in suggestions and defaults, usable in auto-documentation

  • "Subclassing" - created primitives should contain a "subclasser" function that returns a new primitive with additional classes and a different name

  • Pass-through events, and otherwise make the primitives behave more like actual primitives