Created
November 18, 2024 12:07
-
-
Save 03balogun/4c472c9d7e376fe73984b143d7cb57e7 to your computer and use it in GitHub Desktop.
react-pdf-tailwind-primitives.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import {createTw} from "react-pdf-tailwind"; | |
| import {Image, Link, Text, View} from "@react-pdf/renderer"; | |
| import {ComponentProps} from "react"; | |
| import {Style, SVGPresentationAttributes} from "@react-pdf/types"; | |
| type PropsWithClassName<T> = T & {className?: string} | |
| const mergeStyles = (...styles: (Style | Style[] | SVGPresentationAttributes | undefined)[]) => { | |
| const merged: Style = {}; | |
| for (const style of styles) { | |
| if (style) { | |
| if (Array.isArray(style)) { | |
| Object.assign(merged, ...style); | |
| } else { | |
| Object.assign(merged, style); | |
| } | |
| } | |
| } | |
| return merged; | |
| } | |
| const tw = createTw({ | |
| theme: { | |
| fontFamily: { | |
| sans: ["Work Sans"], | |
| } | |
| }, | |
| }); | |
| const twToStyle = (className?: string) => { | |
| return className?.trim() ? tw(className) : undefined | |
| } | |
| export const PView = ({ className, style, ...props}:PropsWithClassName<ComponentProps<typeof View>>) => { | |
| return <View style={mergeStyles(twToStyle(className), style)} {...props}></View> | |
| } | |
| export const PText = ({ className , style, ...props}:PropsWithClassName<ComponentProps<typeof Text>>) => { | |
| return <Text style={mergeStyles(twToStyle(className), style)} {...props}></Text> | |
| } | |
| export const PImage = ({ className, style, ...props}:PropsWithClassName<ComponentProps<typeof Image>>) => { | |
| return <Image style={mergeStyles(twToStyle(className), style)} {...props} /> | |
| } | |
| export const PLink = ({ className, style, ...props}:PropsWithClassName<ComponentProps<typeof Link>>) => { | |
| return <Link style={mergeStyles(twToStyle(className), style)} {...props} /> | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You are a lifesaver man 🔥🙌🏿🫡