Skip to content

Commit

Permalink
fix: align icon
Browse files Browse the repository at this point in the history
  • Loading branch information
LamaEats committed Mar 13, 2024
1 parent 030fdc8 commit ff292b3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
36 changes: 22 additions & 14 deletions src/components/BaseIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,36 @@ export const iconSizesMap = {

export interface BaseIconProps extends React.HTMLAttributes<HTMLSpanElement> {
size: keyof typeof iconSizesMap | number;
value: React.FunctionComponent<React.SVGAttributes<SVGElement>>;
value: React.FC<React.SVGAttributes<SVGElement>>;
color?: string;
stroke?: number;
}

export const BaseIcon = React.forwardRef<HTMLSpanElement, BaseIconProps>(
({ size, value: Component, color, stroke = 1, onClick, className, ...props }, ref) => {
const sizePx = `${typeof size === 'string' ? iconSizesMap[size] : size}px`;
let classes = 'TaskanyIconsWrapper';
const calculateClasses = (map: Record<string, boolean>): string => {
let classNames = '';

if (onClick) {
classes += ' TaskanyIconsWrapper_hover';
for (const key in map) {
if (key in map) {
if (map[key]) {
classNames += `${key} `;
}
}
}

if (className) {
classes += ` ${className}`;
}
return classNames;
};

export const BaseIcon = React.forwardRef<HTMLSpanElement, BaseIconProps>(
({ size, value: Component, color, stroke = 1, onClick, className, ...props }, ref) => {
const sizePx = `${typeof size === 'string' ? iconSizesMap[size] : size}px`;
const classes = calculateClasses({
TaskanyIconsWrapper: true,
TaskanyIconsWrapper_hover: !!onClick,
...(className ? { [className]: true } : null),
});

const style = useMemo<React.CSSProperties>(() => ({
fontSize: sizePx,
width: sizePx,
height: sizePx,
'--taskany-icons-font-size': sizePx,
color,
}), [color, sizePx]);

Expand All @@ -43,7 +51,7 @@ export const BaseIcon = React.forwardRef<HTMLSpanElement, BaseIconProps>(
onClick={onClick}
{...props}
>
<Component className="TaskanyIconsInner" width={sizePx} height={sizePx} strokeWidth={stroke} />
<Component className="TaskanyIconsInner" width="1em" height="1em" strokeWidth={stroke} />
</span>
);
},
Expand Down
6 changes: 5 additions & 1 deletion style.css
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
.TaskanyIconsWrapper {
display: inline-block;
width: 1em;
height: 1em;
font-size: var(--taskany-icons-font-size, 1em);
line-height: 1;
}

.TaskanyIconsWrapper_hover {
cursor: pointer;
}

.TaskanyIconsInner {
vertical-align: middle;
vertical-align: -0.125em;
}

0 comments on commit ff292b3

Please sign in to comment.