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-functional-props

v0.4.0

Published

Handle functional component props properly

Downloads

33

Readme

vue-functional-props

Handle functional component props properly

travis npm size code quality

It's a small side-effect-free library with a single purpose - provide Vue 3 devs a simple way of type-safe handling properties for functional components.

Installation

npm i -S vue-functional-props
or
yarn add vue-functional-props

Usage

npm:

npm i -S vue-functional-props

browser:

<!-- ES2015 -->
<script type="module">
  import { component } from 'https://unpkg.com/vue-functional-props';

  // use it here
</script>

<!-- ES5 with IE11+ general syntax polyfills, global object - `vue-functional-props` -->
<!-- Polyfill `window.Promise` and `Object.assign` yourself! -->
<script src="https://unpkg.com/vue-functional-props/dist/umd.js"></script>

Importing:

// TS-module (pure typescript),
// allows compilation settings to be set from the project config
import { component } from 'vue-functional-props/src';

// ES-module (npm/node, typescript)
import { component } from 'vue-functional-props';

// ESNext (no polyfills for esnext)
import { component } from 'vue-functional-props/dist/esnext';

// ES-module (browser, node)
import { component } from 'https://unpkg.com/vue-functional-props';

// Classic node commonjs
const { component } = require('vue-functional-props/dist/js');

Documenation example

Official Vue 3 docs state that functional component props can only be added in one way:

import { h } from 'vue';

// here, in TS, a user has to define props separately in two places, which produces code duplication
const DynamicHeading = (props: { level: number }, context) => {
  return h(`h${props.level}`, context.attrs, context.slots);
};

DynamicHeading.props = {
  level: Number;
};

export default DynamicHeading;

This is far from perfect for user experience, and is definitely in need of some sort of a wrapper (with type inference, preferably).

This tiny (~300B gzipped) library allows to achieve just that!

import { h } from 'vue';
import { withProps } from 'vue-functional-props'

// No code duplication whatsoever!
export default withProps({
  level: Number,
}, (props, context) => {
  // here props.level is already defined
  return h(`h${props.level}`, context.attrs, context.slots);
});

API

import { component, withProps, prop } from 'vue-functional-props';

withProps

function withProps<P, S>(props: P, setup: S): S

A simple function wrapper that accepts a standard vue props object definition and a setup function and adds props to the setup function definition so they can be recognized by vue.

Usage with object prop notation:

withProps({
  level: Number,
  someProp: {
    type: String,
    required: true
  },
  otherProp: {
    type: String,
    default: ''
  }
}, (props, context) => {
  props.level // number | undefined
  props.someProp // string
  props.otherProp // string | undefined

  return h(`h${props.level}`, context.attrs, context.slots);
});

Usage with an array notation:

withProps(
  // `as const` cast is needed for array notation in order for TS to infer the type
  ['level', 'someProp', 'otherProp'] as const,
  (props, context) => {
    // No way around `any` if using array notation
    props.level // any
    props.someProp // any
    props.otherProp // any

    return h(`h${props.level}`, context.attrs, context.slots);
  }
);

component

function component(name: string, inheritAttrs: boolean = true)

Useful for when you need to define name, events and other properties,
but still need a functional component.

Note: it doesn't call defineComponent!

Returns a type-safe functional component builder with the following methods:

  • withProps - identical to the exported withProps, but accepts only one argument - props definition. Returns the same object as the component function.
  • emits - accepts a map of event declaration like this. Returns the same object as the component function.
  • setup - accepts the functional component itself, providing type-safety. Returns the component itself. Must be called last.
import { h } from 'vue';
import { component } from 'vue-functional-props';

export default component(
  /* name:         */ 'DynamicHeading',
  /* inheritAttrs: */ false
)
  .emits({
    click(e: MouseEvents) {}
  })
  .withProps({
    level: {
      type: [Number, String],
      required: true,
      validator: level => Number(level) > 0 && Number(level) <= 6
    }
  })
  .setup((props, context) => {
    // here props.level is defined and typed as `number | string`
    return h(`h${props.level}`, {
      ...context.attrs,
      on: {
        // Here, `emit` is typed, and event name is autocompleted
        click: e => context.emit('click', e)
      }
    }, context.slots);
  });

/* Equivalent to:
const DynamicHeading = (props, context) => {
  // Absolutely no typesafety here, everything is `any`
  return h(`h${props.level}`, {
    ...context.attrs,
    on: {
      click: e => context.emit('click', e)
    }
  }, context.slots);
};

DynamicHeading.displayName = 'DynamicHeading';
DynamicHeading.inheritAttrs = false;
DynamicHeading.props = {
  level: {
    type: [Number, String],
    required: true,
    validator: level => Number(level) > 0 && Number(level) <= 6
  }
};
DynamicHeading.emits = {
  click(e: MouseEvents) {}
};

export default DynamicHeading;
*/

withProps and emits can be called in any order or not called at all:

// Valid
component('DynamicHeading', false)
  .withProps(['level'])
  .setup((props, context) => {
    return h(`h${props.level}`, {
      ...context.attrs,
      on: { click: e => context.emit('click', e) }
    }, context.slots);
  });


// Also Valid
component('DynamicHeading', false)
  .emits({ click(e: MouseEvent) {} })
  .setup((props, context) => {
    return h(`h${context.attrs.level}`, {
      ...context.attrs,
      on: { click: e => context.emit('click', e) }
    }, context.slots);
  });

// Also Valid
component('DynamicHeading', false)
  .setup((props, context) => {
    return h(`h${context.attrs.level}`, {
      ...context.attrs,
      on: { click: e => context.emit('click', e) }
    }, context.slots);
  });

// INVALID!
component('DynamicHeading', false)
  .setup((props, context) => {
    return h(`h${context.attrs.level}`, {
      ...context.attrs,
      on: { click: e => context.emit('click', e) }
    }, context.slots);
  })
  .emits({ click(e: MouseEvent) {} });
// .setup must be called last!

prop

function prop<T, D = T>(options: Prop<T, D>): () => Prop<T, D>

T - a complex type for the prop.
D - provide if default differs from T.

Enables type validaton for complex types in props without the need to pass constructors or runtime validators.
Basically, a NOOP without TypeScript.

Currently needs to be an empty function, that retutns a function which accepts the prop options, like this:

prop<SomeType>(
  // Empty function call
)({
  // Real function call with prop options
  type: Object
})

This is needed to infer the option type correctly due to a design flaw in TypeScript.

import { prop, withProps } from 'vue-functional-props';

// Some complex objects, for example
export declare interface TTableRow {}
export declare interface ITableColumn {}

export default withProps({
  /**
   * The collection of rows for the table
   */
  rows: prop<TTableRow[]>()({
    type: Array,
    default: () => [],
  }),

  /**
   * Collection of columns to be displayed
   */
  columns: prop<ITableColumn[]>()({
    type: Array,
    default: () => [],
  }),
}, (props) => {
  props.rows // TTableRow[]
  props.columns // ITableColumn[]

  return () => /* some render function */;
});

Contribute

First, fork the repo and clone it:

git clone https://github.com/%your-github-username%/vue-functional-props.git

Then:

npm install

Then:

npm run dev

Then introduce changes and propose a PR!
I'll be happy to review it!


Something's missing or found a bug?
Feel free to create an issue!