Skip to content

Commit

Permalink
Merge pull request #30 from franck-co/main
Browse files Browse the repository at this point in the history
support named slots with styled components
  • Loading branch information
wmertens authored Jul 7, 2024
2 parents 830a774 + 02c338d commit 32afd85
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/qwik-styled.qwik.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import {FunctionComponent, jsx, type IntrinsicElements} from '@builder.io/qwik'
import {
Slot,
component$,
jsx,
type Component,
type PropsOf,
} from '@builder.io/qwik'

// Copied from HtmlIntrinsiceElements because I couldn't make `keyof QwikIntrinsicElements` work well
export type Tags =
Expand Down Expand Up @@ -120,8 +126,8 @@ export type Tags =
| 'wbr'
| 'webview'

export type QwikStyledComponent<Tag extends Tags = 'div'> = FunctionComponent<
IntrinsicElements[Tag]
export type QwikStyledComponent<Tag extends Tags = 'div'> = Component<
PropsOf<Tag>
> & {class: string}

export const isStyled = (o: any): o is QwikStyledComponent =>
Expand All @@ -130,7 +136,7 @@ export const isStyled = (o: any): o is QwikStyledComponent =>
export const styled = <Tag extends Tags>(
Tag: Tag,
myClassName: string
): FunctionComponent<IntrinsicElements[Tag]> & {class: string} => {
): Component<PropsOf<Tag>> & {class: string} => {
const Lite = ({class: extraClass, ...props}: {[x: string]: any}) => {
let classes: string[] | undefined
const check = (cl?: string | {[c: string]: boolean}) => {
Expand All @@ -147,9 +153,13 @@ export const styled = <Tag extends Tags>(
}
check(extraClass)
return jsx(
Tag,
TagComponent,
// @ts-ignore
{...props, class: extraClass ? [myClassName, extraClass] : myClassName}
{
...props,
Tag,
class: extraClass ? [myClassName, extraClass] : myClassName,
}
)
}
// To allow interpolation
Expand All @@ -162,3 +172,10 @@ export const styled = <Tag extends Tags>(
Lite.toString = () => myClassName
return Lite
}

//To allow named slots
const TagComponent = component$<{Tag: string; [x: PropertyKey]: any}>(
({Tag, ...props}) => {
return jsx(Tag, {...props, children: jsx(Slot, {})})
}
)

0 comments on commit 32afd85

Please sign in to comment.