Skip to content

Instantly share code, notes, and snippets.

@Revxrsal
Created August 17, 2023 18:50
Show Gist options
  • Save Revxrsal/ac9fecc2e5b8acfca30070929d7b42dd to your computer and use it in GitHub Desktop.
Save Revxrsal/ac9fecc2e5b8acfca30070929d7b42dd to your computer and use it in GitHub Desktop.
import {JSX, splitProps} from "solid-js";
import {isServer, mergeProps, ssr} from "solid-js/web";
type SVGSVGElementTags = JSX.SVGElementTags["svg"];
export interface IconTree {
a: SVGSVGElementTags;
c: string;
}
export interface IconProps extends SVGSVGElementTags {
size?: string | number;
color?: string;
title?: string;
style?: JSX.CSSProperties;
}
export interface IconBaseProps extends IconProps {
src: IconTree;
}
export declare type IconTypes = (props: IconProps) => JSX.Element;
export const CustomIcon = (props: IconBaseProps) =>
IconTemplate(props.src, props);
export function IconTemplate(iconSrc: IconTree, props: IconProps): JSX.Element {
const mergedProps = mergeProps(iconSrc.a, props) as IconBaseProps;
const [_, svgProps] = splitProps(mergedProps, ["src"]);
return (
<svg
stroke={iconSrc.a.stroke}
color={props.color || "currentColor"}
stroke-width="0"
style={{
...props.style,
overflow: "visible",
}}
{...svgProps}
height={props.size || "1em"}
width={props.size || "1em"}
innerHTML={iconSrc.c}
xmlns="http://www.w3.org/2000/svg"
>
{isServer && ssr(iconSrc.c).t}
{props.title && <title>{props.title}</title>}
</svg>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment