Skip to content

Commit

Permalink
made the icon size helper a forward ref
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-wishes committed Nov 13, 2024
1 parent 9d17623 commit 248556e
Showing 1 changed file with 40 additions and 28 deletions.
68 changes: 40 additions & 28 deletions library/src/components/IconSizeHelper.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@
import { css } from "@emotion/css"
import React, { type ComponentPropsWithoutRef } from "react"
import {
type ForwardedRef,
forwardRef,
type ComponentPropsWithRef,
} from "react"
import { twMerge } from "tailwind-merge"

/**
* IconSizeHelper helps to set a size for an icon.
* It can size spans, svgs and spans with svgs inside.
*/
export function IconSizeHelper({
size,
children,
style,
className,
...props
}: {
size?: number | string
} & ComponentPropsWithoutRef<"div">) {
const sizeProp = typeof size === "number" ? `${size}px` : size
const IconSizeHelper = forwardRef(
(
{
size,
children,
style,
className,
...props
}: {
size?: number | string
} & ComponentPropsWithRef<"div">,
ref: ForwardedRef<HTMLDivElement>,
) => {
const sizeProp = typeof size === "number" ? `${size}px` : size

const centerHelperClass = css`
const centerHelperClass = css`
span {
display: inline-flex;
}
`

const sizeHelperClass = css`
const sizeHelperClass = css`
width: ${sizeProp};
height: ${sizeProp};
Expand All @@ -35,18 +43,22 @@ export function IconSizeHelper({
}
`

return (
<div
className={twMerge(
centerHelperClass,
sizeProp != null ? sizeHelperClass : undefined,
"inline-flex flex-none items-center justify-center",
className,
)}
style={style}
{...props}
>
{children}
</div>
)
}
return (
<div
className={twMerge(
centerHelperClass,
sizeProp != null ? sizeHelperClass : undefined,
"inline-flex flex-none items-center justify-center",
className,
)}
style={style}
ref={ref}
{...props}
>
{children}
</div>
)
},
)

export { IconSizeHelper }

0 comments on commit 248556e

Please sign in to comment.