Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
@polymorphic-factory/solid
Advanced tools
Create polymorphic SolidJS 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
as
prop.This is a deliberate decision to keep the usage as simple as possible.
npm install @polymorphic-factory/solid
or
yarn add @polymorphic-factory/solid
or
pnpm install @polymorphic-factory/solid
Import the polymorphic factory and create your element factory.
import { polymorphicFactory } from '@polymorphic-factory/solid'
const poly = polymorphicFactory()
styled
functionYou can override the default implementation by passing styled
function in the options.
import { Dynamic } from 'solid-js/web'
const poly = polymorphicFactory({
styled: (component, options) => (props) => {
const [local, others] = splitProps(props, ['as'])
const component = local.as || originalComponent
return (
<Dynamic
component={component}
data-custom-styled
data-options={JSON.stringify(options)}
{...others}
/>
)
},
})
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\" }" /> */}
</>
)
}
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>
</>
Use the factory to wrap custom components.
const OriginalComponent = (props) => <div data-original="true" {...props}></div>
const MyComponent = poly(OriginalComponent)
const App = () => <MyComponent />
// render <div data-original="true" />
It still supports the as
prop, which would replace the OriginalComponent
.
<MyComponent as="div" />
// renders <div />
import type { HTMLPolymorphicComponents, HTMLPolymorphicProps } from '@polymorphic-factory/solid'
type PolymorphicDiv = HTMLPolymorphicComponents['div']
type DivProps = HTMLPolymorphicProps<'div'>
MIT © Tim Kolberger
FAQs
@polymorphic-factory/solid
We found that @polymorphic-factory/solid demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.