Skip to content

Commit

Permalink
feat: color-icon component
Browse files Browse the repository at this point in the history
  • Loading branch information
mortennordseth committed Sep 29, 2023
1 parent 87cc8e2 commit bf81335
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/assets/color-icon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { SizeProps, useSize } from './utils';
import { useDarkMode } from '@atb/modules/theme';

export type { SizeProps };

export type ColorIconProps = JSX.IntrinsicElements['img'] & {
size?: SizeProps;
darkable?: boolean;
};

export function ColorIcon({
src,
size = 'normal',
darkable = false,
...props
}: ColorIconProps) {
const [isDarkMode] = useDarkMode();
const wh = useSize(size);

if (!darkable) {
const totalPath = `/assets/colors/icons/${src}`;
// eslint-disable-next-line
return <img width={wh} height={wh} src={totalPath} {...props} />;
} else {
const totalPath = `/assets/colors/icons/${specifyDarkOrLight(
src,
isDarkMode,
)}`;

// eslint-disable-next-line
return <img width={wh} height={wh} src={totalPath} {...props} />;
}
}

function specifyDarkOrLight(src: string | undefined, isDarkMode: boolean) {
const paths = src?.split('/') ?? [];
const darkModeString = isDarkMode ? 'dark' : 'light';
return [...paths.slice(0, -1), darkModeString, paths.slice(-1)].join('/');
}
19 changes: 19 additions & 0 deletions src/assets/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useTheme } from '@atb/modules/theme';

export type SizeProps = 'small' | 'normal' | 'large' | 'x-large';

export function useSize(size: SizeProps) {
const theme = useTheme();

switch (size) {
case 'small':
return theme.icon.size.small;
case 'normal':
return theme.icon.size.normal;
case 'large':
return theme.icon.size.large;
case 'x-large':
// @TODO This should in the design system at some point
return 40;
}
}
11 changes: 11 additions & 0 deletions src/utils/css.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function and(...classes: (string | undefined | false)[]): string {
return classes.filter(Boolean).join(' ');
}
export function andIf(classes: Record<string, boolean>): string {
return Object.entries(classes).reduce(function (acc, item) {
if (!item[1]) {
return acc;
}
return acc + ' ' + item[0];
}, '');
}

0 comments on commit bf81335

Please sign in to comment.