Module

x/nano_jsx/withStyles.ts

🎯 SSR first, lightweight 1kB JSX library.
Latest
File
import { h } from './core.ts'import { Component } from './component.ts'import { Fragment } from './fragment.ts'import { Helmet } from './components/helmet.ts'
interface ObjectHasToString { toString: () => string}type Styles = stringtype FunctionReturnsString = () => stringtype WithStylesType = Styles | ObjectHasToString | FunctionReturnsString
export const withStyles: any = (...styles: WithStylesType[]) => (WrappedComponent: any) => { return class extends Component { render() { const { children, ...rest } = this.props
const helmets: any[] = [] styles.forEach(style => { if (typeof style === 'string') { helmets.push(h(Helmet, null, h('style', null, style))) } else if (typeof style === 'function') { const _style = style() if (typeof _style === 'string') { helmets.push(h(Helmet, null, h('style', null, _style))) } } else if (typeof style === 'object') { const _style = style.toString?.() if (typeof _style === 'string') { helmets.push(h(Helmet, null, h('style', null, _style))) } } })
const component = children && children.length > 0 ? h(WrappedComponent, { ...rest }, children) : h(WrappedComponent, { ...this.props })
return h(Fragment, null, ...helmets, component) } } }