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

feat: add simple animations to button icons #14

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
23 changes: 23 additions & 0 deletions src/components/common/Button/cmp.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { StoryFn } from '@storybook/react'
import Button from './cmp'
import { ButtonProps, ButtonSize } from './types'
import { useTheme } from 'styled-components'
import Icon from '../Icon'

// More on default export: https://storybook.js.org/docs/react/writing-stories/introduction#default-export
export default {
Expand Down Expand Up @@ -60,6 +61,28 @@ Forwarded.parameters = {

// ---

export const WithIcon = Template.bind({})
WithIcon.args = {
...defaultArgs,
hover: false,
active: false,
focus: false,
disabled: false,
as: 'a',
variant: 'textOnly',
children: (
<>
Text
<Icon name="square-up-right" size="md" />
</>
),
}
WithIcon.parameters = {
...defaultParams,
}

// ---

function Cell({ children, gc, gr, bg = false }: any) {
return (
<div
Expand Down
3 changes: 2 additions & 1 deletion src/components/common/Button/cmp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ export const Button = forwardRef(
hover,
active,
focus,
animation,
className,
...rest
}: ButtonProps,
ref: ForwardedRef<HTMLButtonElement>,
) => {
// @note: Storybook testing purposes
const classes = useMemo(() => {
return (
[
Expand All @@ -41,6 +41,7 @@ export const Button = forwardRef(
ref,
as,
className: classes,
$animation: animation,
...rest,
}}
>
Expand Down
53 changes: 52 additions & 1 deletion src/components/common/Button/styles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ const defaultVariants = (props: StyledButtonProps) => {
width: auto;
min-width: 0;
max-width: 100%;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
border: none;
Expand Down Expand Up @@ -223,13 +222,63 @@ const disableVariants = (props: StyledButtonProps) => {
`
}

const animationVariants = (props: StyledButtonProps) => {
switch (props.$animation) {
case 'icon-to-right-on-hover':
return css`
> svg,
> img {
transition: transform 0.3s ease;
}

&:hover > svg,
&._hover > svg,
&:hover > img,
&._hover > img {
transform: translateX(2px);
}
`
case 'icon-to-top-on-hover':
return css`
> svg,
> img {
transition: transform 0.3s ease;
}

&:hover > svg,
&._hover > svg,
&:hover > img,
&._hover > img {
transform: translateY(-2px);
}
`
case 'icon-to-top-right-on-hover':
return css`
> svg,
> img {
transition: transform 0.3s ease;
}

&:hover > svg,
&._hover > svg,
&:hover > img,
&._hover > img {
transform: translate(2px, -2px);
}
`
default:
return ''
}
}

export const StyledButton = styled.button<StyledButtonProps>`
${(props) => {
const defaultVariantsCss = defaultVariants(props)
const focusVariantsCss = focusVariants(props)
const hoverVariantsCss = hoverVariants(props)
const activeVariantsCss = activeVariants(props, defaultVariantsCss)
const disableVariantsCss = disableVariants(props)
const animationVariantsCss = animationVariants(props)

return css`
&& {
Expand All @@ -255,6 +304,8 @@ export const StyledButton = styled.button<StyledButtonProps>`
&._disabled {
${disableVariantsCss}
}

${animationVariantsCss}
`
}}
`
6 changes: 6 additions & 0 deletions src/components/common/Button/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@ export type ButtonProps = AnchorHTMLAttributes<HTMLAnchorElement> &

as?: ButtonTag
className?: string

animation?:
| 'icon-to-right-on-hover'
| 'icon-to-top-right-on-hover'
| 'icon-to-top-on-hover'
}

export type StyledButtonProps = {
$kind: string
$variant: string
$size: ButtonSize
$color: string
$animation?: ButtonProps['animation']
} & { theme: DefaultTheme }