Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: icons in Nextjs App Router #758

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 28 additions & 13 deletions src/components/icon-elements/Badge/Badge.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"use client";
import React from "react";
import React, { cloneElement, isValidElement } from "react";
import Tooltip, { useTooltip } from "components/util-elements/Tooltip/Tooltip";
import {
Color,
Expand All @@ -18,14 +18,38 @@ const makeBadgeClassName = makeClassName("Badge");
export interface BadgeProps extends React.HTMLAttributes<HTMLSpanElement> {
color?: Color;
size?: Size;
icon?: React.ElementType;
icon?: React.ElementType | React.ReactElement;
tooltip?: string;
}

const Badge = React.forwardRef<HTMLSpanElement, BadgeProps>((props, ref) => {
const { color, icon, size = Sizes.SM, tooltip, className, children, ...other } = props;

const Icon = icon ? icon : null;
let Icon;
if (icon) {
if (isValidElement(icon)) {
Icon = cloneElement(icon as React.ReactElement, {
className: tremorTwMerge(
makeBadgeClassName("icon"),
"shrink-0 -ml-1 mr-1.5",
iconSizes[size].height,
iconSizes[size].width,
),
});
} else {
const IconElm = icon as React.ElementType;
Icon = (
<IconElm
className={tremorTwMerge(
makeBadgeClassName("icon"),
"shrink-0 -ml-1 mr-1.5",
iconSizes[size].height,
iconSizes[size].width,
)}
/>
);
}
}

const { tooltipProps, getReferenceProps } = useTooltip();

Expand Down Expand Up @@ -61,16 +85,7 @@ const Badge = React.forwardRef<HTMLSpanElement, BadgeProps>((props, ref) => {
{...other}
>
<Tooltip text={tooltip} {...tooltipProps} />
{Icon ? (
<Icon
className={tremorTwMerge(
makeBadgeClassName("icon"),
"shrink-0 -ml-1 mr-1.5",
iconSizes[size].height,
iconSizes[size].width,
)}
/>
) : null}
{Icon}
<span className={tremorTwMerge(makeBadgeClassName("text"), "whitespace-nowrap")}>
{children}
</span>
Expand Down
39 changes: 28 additions & 11 deletions src/components/icon-elements/Icon/Icon.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"use client";
import React from "react";
import React, { cloneElement, isValidElement } from "react";

import Tooltip, { useTooltip } from "components/util-elements/Tooltip/Tooltip";
import { makeClassName, mergeRefs, Sizes, tremorTwMerge } from "lib";
Expand All @@ -17,7 +17,7 @@ export const IconVariants: { [key: string]: IconVariant } = {
};

export interface IconProps extends React.HTMLAttributes<HTMLSpanElement> {
icon: React.ElementType;
icon: React.ElementType | React.ReactElement;
variant?: IconVariant;
tooltip?: string;
size?: Size;
Expand All @@ -34,7 +34,31 @@ const Icon = React.forwardRef<HTMLSpanElement, IconProps>((props, ref) => {
className,
...other
} = props;
const Icon = icon;

let Icon;
if (isValidElement(icon)) {
Icon = cloneElement(icon as React.ReactElement, {
className: tremorTwMerge(
makeIconClassName("icon"),
"shrink-0",
iconSizes[size].height,
iconSizes[size].width,
),
});
} else {
const IconElm = icon as React.ElementType;
Icon = (
<IconElm
className={tremorTwMerge(
makeIconClassName("icon"),
"shrink-0",
iconSizes[size].height,
iconSizes[size].width,
)}
/>
);
}

const iconColorStyles = getIconColors(variant, color);

const { tooltipProps, getReferenceProps } = useTooltip();
Expand All @@ -61,14 +85,7 @@ const Icon = React.forwardRef<HTMLSpanElement, IconProps>((props, ref) => {
{...other}
>
<Tooltip text={tooltip} {...tooltipProps} />
<Icon
className={tremorTwMerge(
makeIconClassName("icon"),
"shrink-0",
iconSizes[size].height,
iconSizes[size].width,
)}
/>
{Icon}
</span>
);
});
Expand Down
57 changes: 41 additions & 16 deletions src/components/input-elements/BaseInput.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
"use client";
import React, { ReactNode, useCallback, useRef, useState } from "react";
import React, {
ReactNode,
cloneElement,
isValidElement,
useCallback,
useRef,
useState,
} from "react";
import { ExclamationFilledIcon, EyeIcon, EyeOffIcon } from "assets";
import { getSelectButtonColors, hasValue } from "components/input-elements/selectUtils";
import { mergeRefs, tremorTwMerge } from "lib";
Expand All @@ -8,7 +15,7 @@ export interface BaseInputProps extends React.InputHTMLAttributes<HTMLInputEleme
type?: "text" | "password" | "email" | "url" | "number";
defaultValue?: string | number;
value?: string | number;
icon?: React.ElementType | React.JSXElementConstructor<any>;
icon?: React.ElementType | React.JSXElementConstructor<any> | React.ReactElement;
error?: boolean;
errorMessage?: string;
disabled?: boolean;
Expand Down Expand Up @@ -43,7 +50,37 @@ const BaseInput = React.forwardRef<HTMLInputElement, BaseInputProps>((props, ref
[isPasswordVisible, setIsPasswordVisible],
);

const Icon = icon;
let Icon;
if (icon) {
if (isValidElement(icon)) {
Icon = cloneElement(icon as React.ReactElement, {
className: tremorTwMerge(
makeInputClassName("icon"),
// common
"shrink-0 h-5 w-5 mx-2.5 absolute left-0 flex items-center",
// light
"text-tremor-content-subtle",
// light
"dark:text-dark-tremor-content-subtle",
),
});
} else {
const IconElm = icon as React.ElementType | React.JSXElementConstructor<any>;
Icon = (
<IconElm
className={tremorTwMerge(
makeInputClassName("icon"),
// common
"shrink-0 h-5 w-5 mx-2.5 absolute left-0 flex items-center",
// light
"text-tremor-content-subtle",
// light
"dark:text-dark-tremor-content-subtle",
)}
/>
);
}
}

const inputRef = useRef<HTMLInputElement>(null);

Expand Down Expand Up @@ -96,19 +133,7 @@ const BaseInput = React.forwardRef<HTMLInputElement, BaseInputProps>((props, ref
className,
)}
>
{Icon ? (
<Icon
className={tremorTwMerge(
makeInputClassName("icon"),
// common
"shrink-0 h-5 w-5 mx-2.5 absolute left-0 flex items-center",
// light
"text-tremor-content-subtle",
// light
"dark:text-dark-tremor-content-subtle",
)}
/>
) : null}
{Icon}
<input
ref={mergeRefs([inputRef, ref])}
defaultValue={defaultValue}
Expand Down
33 changes: 23 additions & 10 deletions src/components/input-elements/Button/Button.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";
import Tooltip, { useTooltip } from "components/util-elements/Tooltip/Tooltip";
import React, { useEffect } from "react";
import React, { cloneElement, isValidElement, useEffect } from "react";
import { useTransition, type TransitionStatus } from "react-transition-state";

import { HorizontalPositions, makeClassName, mergeRefs, Sizes, tremorTwMerge } from "lib";
Expand All @@ -15,7 +15,7 @@ export interface ButtonIconOrSpinnerProps {
loading: boolean;
iconSize: string;
iconPosition: string;
Icon: React.ElementType | undefined;
icon: React.ReactElement | undefined;
needMargin: boolean;
transitionStatus: TransitionStatus;
}
Expand All @@ -24,12 +24,10 @@ export const ButtonIconOrSpinner = ({
loading,
iconSize,
iconPosition,
Icon,
icon,
needMargin,
transitionStatus,
}: ButtonIconOrSpinnerProps) => {
Icon = Icon!;

const margin = !needMargin
? ""
: iconPosition === HorizontalPositions.Left
Expand All @@ -45,6 +43,13 @@ export const ButtonIconOrSpinner = ({
exited: defaultSpinnerSize,
};

let Icon: React.ReactElement | null = null;
if (icon) {
Icon = cloneElement(icon, {
className: tremorTwMerge(makeButtonClassName("icon"), "shrink-0", iconSize, margin),
});
}

return loading ? (
<LoadingSpinner
className={tremorTwMerge(
Expand All @@ -57,12 +62,12 @@ export const ButtonIconOrSpinner = ({
style={{ transition: `width 150ms` }}
/>
) : (
<Icon className={tremorTwMerge(makeButtonClassName("icon"), "shrink-0", iconSize, margin)} />
Icon
);
};

export interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
icon?: React.ElementType;
icon?: React.ElementType | React.ReactElement;
iconPosition?: HorizontalPosition;
size?: Size;
color?: Color;
Expand All @@ -89,7 +94,15 @@ const Button = React.forwardRef<HTMLButtonElement, ButtonProps>((props, ref) =>
...other
} = props;

const Icon = icon;
let Icon: React.ReactElement | undefined;
if (icon) {
if (isValidElement(icon)) {
Icon = icon as React.ReactElement;
} else {
const IconElm = icon as React.ElementType;
Icon = <IconElm />;
}
}

const isDisabled = loading || disabled;
const showButtonIconOrSpinner = Icon !== undefined || loading;
Expand Down Expand Up @@ -154,7 +167,7 @@ const Button = React.forwardRef<HTMLButtonElement, ButtonProps>((props, ref) =>
loading={loading}
iconSize={iconSize}
iconPosition={iconPosition}
Icon={Icon}
icon={Icon}
transitionStatus={transitionState.status}
needMargin={needIconMargin}
/>
Expand All @@ -174,7 +187,7 @@ const Button = React.forwardRef<HTMLButtonElement, ButtonProps>((props, ref) =>
loading={loading}
iconSize={iconSize}
iconPosition={iconPosition}
Icon={Icon}
icon={Icon}
transitionStatus={transitionState.status}
needMargin={needIconMargin}
/>
Expand Down
49 changes: 35 additions & 14 deletions src/components/input-elements/MultiSelect/MultiSelect.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"use client";
import React, { isValidElement, useMemo, useRef, useState } from "react";
import React, { cloneElement, isValidElement, useMemo, useRef, useState } from "react";
import { SelectedValueContext } from "contexts";
import { useInternalState } from "hooks";
import { ArrowDownHeadIcon, SearchIcon, XCircleIcon } from "assets";
Expand All @@ -18,7 +18,7 @@ export interface MultiSelectProps extends React.HTMLAttributes<HTMLInputElement>
placeholder?: string;
placeholderSearch?: string;
disabled?: boolean;
icon?: React.ElementType | React.JSXElementConstructor<any>;
icon?: React.ElementType | React.JSXElementConstructor<any> | React.ReactElement;
required?: boolean;
error?: boolean;
errorMessage?: string;
Expand All @@ -45,7 +45,38 @@ const MultiSelect = React.forwardRef<HTMLInputElement, MultiSelectProps>((props,
} = props;
const listboxButtonRef = useRef<HTMLButtonElement | null>(null);

const Icon = icon;
let Icon: React.ReactElement | undefined;

if (icon) {
if (isValidElement(icon)) {
Icon = cloneElement(icon as React.ReactElement, {
className: tremorTwMerge(
makeMultiSelectClassName("Icon"),
// common
"flex-none h-5 w-5",
// light
"text-tremor-content-subtle",
// dark
"dark:text-dark-tremor-content-subtle",
),
});
} else {
const IconElm = icon as React.ElementType | React.JSXElementConstructor<any>;
Icon = (
<IconElm
className={tremorTwMerge(
makeMultiSelectClassName("Icon"),
// common
"flex-none h-5 w-5",
// light
"text-tremor-content-subtle",
// dark
"dark:text-dark-tremor-content-subtle",
)}
/>
);
}
}

const [selectedValue, setSelectedValue] = useInternalState(defaultValue, value);

Expand Down Expand Up @@ -155,17 +186,7 @@ const MultiSelect = React.forwardRef<HTMLInputElement, MultiSelectProps>((props,
"absolute inset-y-0 left-0 flex items-center ml-px pl-2.5",
)}
>
<Icon
className={tremorTwMerge(
makeMultiSelectClassName("Icon"),
// common
"flex-none h-5 w-5",
// light
"text-tremor-content-subtle",
// dark
"dark:text-dark-tremor-content-subtle",
)}
/>
{Icon}
</span>
)}
<div className="h-6 flex items-center">
Expand Down
Loading
Loading