-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move some function to utils, add test cases
- Loading branch information
1 parent
76b9271
commit 87a4b4b
Showing
4 changed files
with
275 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import color from 'color'; | ||
import type { Theme } from '../../types'; | ||
import { white, black } from '../../styles/themes/v2/colors'; | ||
import type { IconSource } from '../Icon'; | ||
|
||
export const MIN_WIDTH = 112; | ||
export const MAX_WIDTH = 280; | ||
|
||
type ContentProps = { | ||
isV3: boolean; | ||
iconWidth: number; | ||
leadingIcon?: IconSource; | ||
trailingIcon?: IconSource; | ||
}; | ||
|
||
type ColorProps = { | ||
theme: Theme; | ||
disabled?: boolean; | ||
}; | ||
|
||
const getDisabledColor = (theme: Theme) => { | ||
if (theme.isV3) { | ||
return theme.colors.onSurfaceDisabled; | ||
} | ||
|
||
return color(theme.dark ? white : black) | ||
.alpha(0.32) | ||
.rgb() | ||
.string(); | ||
}; | ||
|
||
const getTitleColor = ({ theme, disabled }: ColorProps) => { | ||
if (disabled) { | ||
return getDisabledColor(theme); | ||
} | ||
|
||
if (theme.isV3) { | ||
return theme.colors.onSurface; | ||
} | ||
|
||
return color(theme.colors.text).alpha(0.87).rgb().string(); | ||
}; | ||
|
||
const getIconColor = ({ theme, disabled }: ColorProps) => { | ||
if (disabled) { | ||
return getDisabledColor(theme); | ||
} | ||
|
||
if (theme.isV3) { | ||
return theme.colors.onSurfaceVariant; | ||
} | ||
|
||
return color(theme.colors.text).alpha(0.54).rgb().string(); | ||
}; | ||
|
||
export const getMenuItemColor = ({ theme, disabled }: ColorProps) => { | ||
return { | ||
titleColor: getTitleColor({ theme, disabled }), | ||
iconColor: getIconColor({ theme, disabled }), | ||
underlayColor: theme.isV3 | ||
? color(theme.colors.primary).alpha(0.12).rgb().string() | ||
: undefined, | ||
}; | ||
}; | ||
|
||
export const getContentMaxWidth = ({ | ||
isV3, | ||
iconWidth, | ||
leadingIcon, | ||
trailingIcon, | ||
}: ContentProps) => { | ||
if (isV3) { | ||
if (leadingIcon && trailingIcon) { | ||
return MAX_WIDTH - (2 * iconWidth + 24); | ||
} | ||
|
||
if (leadingIcon || trailingIcon) { | ||
return MAX_WIDTH - (iconWidth + 24); | ||
} | ||
|
||
return MAX_WIDTH - 12; | ||
} | ||
|
||
if (leadingIcon) { | ||
return MAX_WIDTH - (iconWidth + 48); | ||
} | ||
|
||
return MAX_WIDTH - 16; | ||
}; |
Oops, something went wrong.