-
Notifications
You must be signed in to change notification settings - Fork 27
feat: experimental IconButton #485
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fabc108
chore: adding some tests
lloydaf fc6a2ea
fix: trying to fix the styles
lloydaf 59fbf3a
fix: fixing the styles and updating the tests
lloydaf ba749c3
fix: fixing comment syntax and styles
lloydaf d1a69b3
fix: fixing the sizes and inheriting the properties to avoid redefining
lloydaf 68b52a3
fix: using flexbox
lloydaf 30079a0
fix: adding exports
lloydaf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
src/components/experimental/IconButton/IconButton.spec.tsx
This file contains hidden or 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,46 @@ | ||
| import * as React from 'react'; | ||
| import { render, screen } from '@testing-library/react'; | ||
| import { IconButton } from './IconButton'; | ||
| import { TrashIcon } from '../../../icons'; | ||
|
|
||
| describe('Experimental: IconButton', () => { | ||
| it('renders an icon button with the provided icon', () => { | ||
| const onPress = jest.fn(); | ||
| render(<IconButton onPress={onPress} Icon={TrashIcon} />); | ||
| expect(screen.getByTestId('standard-icon-container')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('calls onPress when clicked', () => { | ||
| const onPress = jest.fn(); | ||
| render(<IconButton Icon={TrashIcon} onPress={onPress} />); | ||
| screen.getByTestId('standard-icon-container').click(); | ||
| expect(onPress).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('does not call onPress when disabled', () => { | ||
| const onPress = jest.fn(); | ||
| render(<IconButton Icon={TrashIcon} onPress={onPress} isDisabled />); | ||
| screen.getByTestId('standard-icon-container').click(); | ||
| expect(onPress).toHaveBeenCalledTimes(0); | ||
| }); | ||
|
|
||
| it('sets the right sizes for standard variant', () => { | ||
| const onPress = jest.fn(); | ||
| render(<IconButton Icon={TrashIcon} onPress={onPress} />); | ||
| const iconContainerInstance = screen.getByTestId('standard-icon-container'); | ||
| const containerStyle = window.getComputedStyle(iconContainerInstance); | ||
| expect(containerStyle.width).toBe('2.5rem'); | ||
| expect(containerStyle.height).toBe('2.5rem'); | ||
| expect(containerStyle.borderRadius).toBe('100%'); | ||
| }); | ||
|
|
||
| it('sets the right sizes for tonal variant', () => { | ||
| const onPress = jest.fn(); | ||
| render(<IconButton Icon={TrashIcon} onPress={onPress} variant="tonal" />); | ||
| const iconContainerInstance = screen.getByTestId('tonal-icon-container'); | ||
| const containerStyle = window.getComputedStyle(iconContainerInstance); | ||
| expect(containerStyle.width).toBe('3.5rem'); | ||
| expect(containerStyle.height).toBe('3.5rem'); | ||
| expect(containerStyle.borderRadius).toBe('100%'); | ||
| }); | ||
| }); |
This file contains hidden or 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,121 @@ | ||
| import React from 'react'; | ||
| import styled from 'styled-components'; | ||
| import { ButtonProps, Button } from 'react-aria-components'; | ||
| import { IconProps } from '../../../icons'; | ||
| import { getSemanticValue } from '../../../essentials/experimental'; | ||
|
|
||
| export interface IconButtonProps extends ButtonProps { | ||
| isActive?: boolean; | ||
| variant?: 'standard' | 'tonal'; | ||
| Icon: React.FC<IconProps>; | ||
| onPress: () => void; | ||
| } | ||
|
|
||
| const StandardIconContainer = styled(Button)<Omit<IconButtonProps, 'Icon'>>` | ||
| height: 2.5rem; | ||
| width: 2.5rem; | ||
| border-radius: 100%; | ||
| background-color: transparent; | ||
| border-color: transparent; | ||
|
|
||
| /* we create a before pseudo element to mess with the opacity (see the hovered state) */ | ||
| &::before { | ||
| position: absolute; | ||
| content: ''; | ||
| border-radius: inherit; | ||
| opacity: 0; | ||
| height: inherit; | ||
| width: inherit; | ||
| } | ||
|
|
||
| /* we want to change the opacity here but not affect the icon, so we have to use the before pseudo element */ | ||
| &[data-hovered]::before { | ||
| opacity: 0.16; | ||
| background-color: ${getSemanticValue('on-surface')}; | ||
| } | ||
|
|
||
| display: flex; | ||
| align-items: center; | ||
| justify-content: center; | ||
|
|
||
| &:not([data-disabled]) { | ||
| color: ${props => (props.isActive ? getSemanticValue('interactive') : getSemanticValue('on-surface'))}; | ||
| } | ||
|
|
||
| &[data-disabled] { | ||
| opacity: 0.38; | ||
| } | ||
| `; | ||
|
|
||
| const TonalIconContainer = styled(Button)<Omit<IconButtonProps, 'Icon'>>` | ||
| height: 3.5rem; | ||
| width: 3.5rem; | ||
| border-radius: 100%; | ||
| border-color: transparent; | ||
| background: none; | ||
|
|
||
| /* we create a before pseudo element to mess with the opacity (see the hovered state) */ | ||
| &::before { | ||
| position: absolute; | ||
| content: ''; | ||
| border-radius: inherit; | ||
| height: inherit; | ||
| width: inherit; | ||
| background-color: ${props => | ||
| props.isActive && !props.isDisabled | ||
| ? getSemanticValue('interactive-container') | ||
| : getSemanticValue('surface')}; | ||
| z-index: -1; | ||
| } | ||
|
|
||
| /* we want to change the opacity here but not affect the icon, so we have to use the before pseudo element */ | ||
| &[data-hovered]::before { | ||
| background-color: color-mix( | ||
| in hsl, | ||
| ${getSemanticValue('on-surface')} 100%, | ||
| ${props => (props.isActive ? getSemanticValue('interactive-container') : getSemanticValue('on-surface'))} | ||
| 100% | ||
| ); | ||
| opacity: 0.16; | ||
| } | ||
|
|
||
| display: flex; | ||
| align-items: center; | ||
| justify-content: center; | ||
|
|
||
| &:not([data-disabled]) { | ||
| color: ${props => | ||
| props.isActive ? getSemanticValue('on-interactive-container') : getSemanticValue('on-surface')}; | ||
| } | ||
|
|
||
| &[data-disabled] { | ||
| opacity: 0.38; | ||
| } | ||
| `; | ||
|
|
||
| export const IconButton = ({ | ||
| isDisabled = false, | ||
| isActive = false, | ||
| Icon, | ||
| variant = 'standard', | ||
| onPress | ||
| }: IconButtonProps) => | ||
| variant === 'standard' ? ( | ||
| <StandardIconContainer | ||
| data-testid="standard-icon-container" | ||
| onPress={onPress} | ||
| isDisabled={isDisabled} | ||
| isActive={isActive} | ||
| > | ||
| <Icon data-testid="iconbutton-icon" /> | ||
| </StandardIconContainer> | ||
| ) : ( | ||
| <TonalIconContainer | ||
| data-testid="tonal-icon-container" | ||
| onPress={onPress} | ||
| isDisabled={isDisabled} | ||
| isActive={isActive} | ||
| > | ||
| <Icon data-testid="iconbutton-icon" /> | ||
| </TonalIconContainer> | ||
| ); | ||
47 changes: 47 additions & 0 deletions
47
src/components/experimental/IconButton/docs/IconButton.stories.tsx
This file contains hidden or 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,47 @@ | ||
| import { StoryObj, Meta } from '@storybook/react'; | ||
| import { IconButton } from '../IconButton'; | ||
| import { TrashIcon } from '../../../../icons'; | ||
|
|
||
| const meta: Meta = { | ||
| title: 'Experimental/Components/IconButton', | ||
| component: IconButton, | ||
| parameters: { | ||
| layout: 'centered' | ||
| }, | ||
| args: { | ||
| Icon: TrashIcon, | ||
| onPress: () => alert('Clicked!'), | ||
| isDisabled: false | ||
| } | ||
| }; | ||
|
|
||
| export default meta; | ||
|
|
||
| type Story = StoryObj<typeof IconButton>; | ||
|
|
||
| export const Default: Story = {}; | ||
|
|
||
| export const Disabled: Story = { | ||
| args: { | ||
| isDisabled: true | ||
| } | ||
| }; | ||
|
|
||
| export const Active: Story = { | ||
| args: { | ||
| isActive: true | ||
| } | ||
| }; | ||
|
|
||
| export const Tonal: Story = { | ||
| args: { | ||
| variant: 'tonal' | ||
| } | ||
| }; | ||
|
|
||
| export const TonalActive: Story = { | ||
| args: { | ||
| variant: 'tonal', | ||
| isActive: true | ||
| } | ||
| }; |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.