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-tsx-macros

v1.3.4

Published

Set of marcos for convenient definitions of Vue JSX components. Enhances developer experience by removing duplication and improving type inference. Write type-safe components with less extra tooling (like `vue-tsc`/`Volar`).

Downloads

12

Readme

vue-tsx-macros

Description

Set of marcos for convenient definitions of Vue JSX components. Enhances developer experience by removing duplication and improving type inference. Write type-safe components with less extra tooling (like vue-tsc/Volar).

Includes

  • typescript types
  • babel plugin for code transformation

Usage examples

Sandbox

Example project

Features

  • component$

    • infers component name from function or variable name
    • infers component props similar to defineProps() and withDefaults() from vue
    • infers component emits similar to defineEmits() from vue
  • useRender$

    • allows to expose instance properties with correct type inference and at the same time use render function inside setup
  • defaultProps$

    • adds type assistance for default props definition, in case you want to reuse them outside of withDefaults()

How to use

  1. Install
npm install vue-tsx-macros
  1. Include babel plugin into your build configuration
  • Vite

Add babel plugin to @vitejs/plugin-vue-jsx options

import vueJsx from "@vitejs/plugin-vue-jsx";

export default {
  plugins: [
    vueJsx({
      babelPlugins: ["vue-tsx-macros/babel-plugin"],
    }),
  ],
};

If you are using nuxt with vite - use extendViteConfig.

  • Babel

Add plugin to your babel config alongside @vue/babel-plugin-jsx

{
  "plugins": ["@vue/babel-plugin-jsx", "vue-tsx-macros/babel-plugin"]
}

Plugin options

{
  /**
   * If false, usage of emits argument in `component$` will throw compile-time error
   * @default true
   */
  allowEmits: boolean;
}
  1. Use
import { ref } from "vue";
import { component$, useRender$ } from "vue-tsx-macros";

export const Example = component$().define((props, { emit }) => {
  const counterRef = ref<InstanceType<typeof Counter>>();

  onMounted(() => {
    const id = setInterval(() => {
      counterRef.value?.increment();
    }, 5000);

    onUnmounted(() => clearInterval(id));
  });

  return () => (
    <div>
      {"A counter"}
      <Counter ref={counterRef} />
    </div>
  );
});

type CounterProps = {
  initial?: number;
};

type CounterEmits = {
  update: (value: number) => void;
};

const Counter = component$<ExampleProps, ExampleEmits>().define(
  (props, { emit }) => {
    const count = ref(props.initial || 0);

    const increment = (value = 1) => {
      count.value += value;
      emit("update", counter.value);
    };

    useRender$(() => (
      <button onClick={() => increment()}>{count.value}</button>
    ));

    return { increment };
  }
);
Counter.inheritAttrs = false;

Transform examples

  • Standard component
export const Example = component$<{ initialValue?: number }>().define(
  (props) => {
    const counter = ref(props.initialValue || 0);
    const onClick = () => (counter.value += 1);
    return () => <button onClick={onClick}>{counter.value}</button>;
  }
);
export const Example = {
  name: "Example",
  props: ["initialValue"],
  setup: (props) => {
    const counter = ref(props.initialValue || 0);
    const onClick = () => (counter.value += 1);
    return () => <button onClick={onClick}>{counter.value}</button>;
  },
};
  • With defaultProps
export const Example = component$<{ initialValue?: number }>()
  .withDefaults({ initialValue: 0 })
  .define((props) => {
    const counter = ref(props.initialValue);
    const onClick = () => (counter.value += 1);
    return () => <button onClick={onClick}>{counter.value}</button>;
  });
const _defaultProps = {
  initialValue: 0,
};
export const Example = defineComponent({
  name: "Example",
  props: {
    initialValue: {
      type: null,
      default: _defaultProps["initialValue"],
    },
  },
  setup: (props) => {
    const counter = ref(props.initialValue);
    const onClick = () => counter.value + 1;
    return () => <button onClick={onClick}>{counter.value}</button>;
  },
});
  • With emits
type Props = {
  initialValue: number;
};

type Emits = {
  update: (value: number) => void;
};

export const Example = component$<Props, Emits>().define((props, { emit }) => {
  const counter = ref(props.initialValue);
  const onClick = () => {
    counter.value += 1;
    emit("update", counter.value);
  };
  return () => <button onClick={onClick}>{counter.value}</button>;
});
type Props = {
  initialValue: number,
};
type Emits = {
  update: (value: number) => void,
};
export const Example = defineComponent({
  name: "Example",
  props: ["initialValue"],
  emits: ["update"],
  setup: (props, { emit }) => {
    const counter = ref(props.initialValue);
    const onClick = () => {
      counter.value += 1;
      emit("update", counter.value);
    };
    return () => <button onClick={onClick}>{counter.value}</button>;
  },
});
  • Expose properties
export const Example = component$().define(() => {
  const counter = ref(0);

  const increment = () => (counter.value += 1);

  useRender$(() => <button onClick={increment}>{counter.value}</button>);

  return { increment };
});
let _render;
export const Example = defineComponent({
  name: "Example",
  setup: () => {
    const counter = ref(0);
    const increment = () => (counter.value += 1);
    _render = () => <button onClick={increment}>{counter.value}</button>;
    return {
      increment,
    };
  },
  render() {
    return _render();
  },
});
  • Functional component
export const Example = component$<{ size?: number }>().functional(
  ({ size = 500 }) => {
    return <div data-size={size} />;
  }
);
export const Example = Object.assign(
  ({ size = 500 }) => {
    return <div data-size={size} />;
  },
  {
    displayName: "Example",
    props: ["size"],
  }
);

Limitations

  • In component$, type parameters for props and emits must be TypeLiteral or TypeReference to InterfaceDeclaration or TypeAliasDeclaration with TypeLiteral in the same file (similar to defineProps() from vue). Note that only explicitly enumerated properties are included.
// ok
export const Example = component$<{ size: number }>().define(...)

// ok
export interface ExampleProps {
  size: number
}
export const Example = component$<Props>().define(...)

// ok
export type ExampleProps = {
  size: number
}
export const Example = component$<Props>().define(...)

// compile-time error
import { SomeProps } from './some-file'
export const Example = component$<SomeProps>().define(...)

// compile-time error
export type ExampleProps = { size: number } & { extra: number }
export const Example = component$<ExampleProps>().define(...)
  • useRender$ must be used only in component$().define( function.
// ok
export const Example = component$().define(() => {
  const counter = ref(0);
  const increment = () => (counter.value += 1);

  useRender$(() => <button onClick={increment}>{counter.value}</button>);

  return { increment };
});

// compile-time error
export const Example = component$().define((props) => {
  const counter = ref(0);
  const increment = () => {
    counter.value += 1;

    useRender$(() => <button onClick={increment}>{counter.value}</button>);
  };
  return { increment };
});