Skip to content

Instantly share code, notes, and snippets.

@03balogun
Created November 18, 2024 12:07
Show Gist options
  • Select an option

  • Save 03balogun/4c472c9d7e376fe73984b143d7cb57e7 to your computer and use it in GitHub Desktop.

Select an option

Save 03balogun/4c472c9d7e376fe73984b143d7cb57e7 to your computer and use it in GitHub Desktop.
react-pdf-tailwind-primitives.tsx
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} />
}
@mosco98
Copy link

mosco98 commented Nov 18, 2024

You are a lifesaver man 🔥🙌🏿🫡

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment