@polymorphic-factory/vue
| Install | |
|---|---|
npm install @polymorphic-factory/vue |
|
| Latest Version: | 0.2.1 |
| Vue: | 3 |
| License: | MIT |
| Last Updated: | Oct 20, 2023 |
| Links: | GitHub · npm |
@polymorphic-factory/vue
Create polymorphic VueJS components with a customizable styled function.
A polymorphic component is a component that can be rendered with a different element.
Known drawbacks for the type definitions:
Event handlers are not typed correctly when using the
asprop.This is a deliberate decision to keep the usage as simple as possible.
Installation
npm install @polymorphic-factory/vue
or
yarn add @polymorphic-factory/vue
or
pnpm install @polymorphic-factory/vue
Usage
Import the polymorphic factory and create your element factory.
import { polymorphicFactory } from '@polymorphic-factory/vue'
const poly = polymorphicFactory()
Custom styled function
You can override the default implementation by passing styled function in the options.
import { defineComponent } from 'vue'
const poly = polymorphicFactory({
styled: (originalComponent, options) =>
defineComponent({
props: ['as'],
setup(props, { slots, attrs }) {
const component = props.as || originalComponent
return () =>
h(
component,
{ 'data-custom-styled': true, 'data-options': JSON.stringify(options), ...attrs },
slots,
)
},
}),
})
const WithOptions = poly('div', { hello: 'world' })
const App = () => {
return (
<>
<poly.div hello="world" />
{/* renders <div data-custom-styled hello="world" /> */}
<WithOptions />
{/* renders <div data-custom-styled data-options="{ \"hello\": \"world\" }" /> */}
</>
)
}
Inline
Use the element factory to create elements inline.
Every JSX element is supported div, main, aside, etc.
<>
<poly.div />
<poly.main>
<poly.section>
<poly.div as="p">This is rendered as a p element</poly.div>
</poly.section>
</poly.main>
</>
Factory
Use the factory to wrap custom components.
const OriginalComponent = defineComponent({
setup(props) {
return () => <div data-original="true" {...props} />
},
})
const MyComponent = poly(OriginalComponent)
const App = h(MyComponent)
// render <div data-original="true" />
It still supports the as prop, which would replace the OriginalComponent.
<MyComponent as="div" />
// renders <div />
Types
import type { HTMLPolymorphicComponents, HTMLPolymorphicProps } from '@polymorphic-factory/vue'
type PolymorphicDiv = HTMLPolymorphicComponents['div']
type DivProps = HTMLPolymorphicProps<'div'>
License
MIT © Tim Kolberger
Version History
| Version | Released | Vue | License |
|---|---|---|---|
| 0.2.1 | >=3 | MIT | |
| 0.2.0 | >=3 | MIT | |
| 0.1.0 | >=3 | MIT |
Pre-releases (110)
| Version | Released | Vue | License |
|---|---|---|---|
| 0.0.0-rc-20230614200801 | >=3 | MIT | |
| 0.0.0-rc-20230613141759 | >=3 | MIT | |
| 0.0.0-rc-20230613085902 | >=3 | MIT | |
| 0.0.0-rc-20230613003612 | >=3 | MIT | |
| 0.0.0-rc-20230612214812 | >=3 | MIT | |
| 0.0.0-rc-20230612115703 | >=3 | MIT | |
| 0.0.0-rc-20230610193006 | >=3 | MIT | |
| 0.0.0-rc-20230610124844 | >=3 | MIT | |
| 0.0.0-rc-20230610063533 | >=3 | MIT | |
| 0.0.0-rc-20230610002950 | >=3 | MIT | |
| 0.0.0-rc-20230609181245 | >=3 | MIT | |
| 0.0.0-rc-20230608051033 | >=3 | MIT | |
| 0.0.0-rc-20230607002905 | >=3 | MIT | |
| 0.0.0-rc-20230605213305 | >=3 | MIT | |
| 0.0.0-rc-20230603043643 | >=3 | MIT | |
| 0.0.0-rc-20230602002236 | >=3 | MIT | |
| 0.0.0-rc-20230601210441 | >=3 | MIT | |
| 0.0.0-rc-20230601125021 | >=3 | MIT | |
| 0.0.0-rc-20230601012146 | >=3 | MIT | |
| 0.0.0-rc-20230531165538 | >=3 | MIT | |
| 0.0.0-rc-20230531153928 | >=3 | MIT | |
| 0.0.0-rc-20230530223749 | >=3 | MIT | |
| 0.0.0-rc-20230530151204 | >=3 | MIT | |
| 0.0.0-rc-20230530114542 | >=3 | MIT | |
| 0.0.0-rc-20230529202751 | >=3 | MIT | |
| 0.0.0-rc-20230527141535 | >=3 | MIT | |
| 0.0.0-rc-20230527021257 | >=3 | MIT | |
| 0.0.0-rc-20230526193454 | >=3 | MIT | |
| 0.0.0-rc-20230526024915 | >=3 | MIT | |
| 0.0.0-rc-20230524204025 | >=3 | MIT |
Showing the latest 30 of 110. See every release on npm