-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FCT-808: Create
CustomIcon
component (#2803)
* feat(CustomIcon): implement initial CustomIcon component with tests and documentation * feat(CustomIcon): update README, remove height/width from fixture, copy sizing values into style file directly, remove warning * feat(customIcon): add overflow:hidden to styles so that svg does not get clipped by border radius * feat(customIcon): update readme based on feedback * feat(customIcon): update flex properties to address resize concerns * feat(customIcon): use inline-block instead of flex due to safari computing svg width as 0px in flex containers where there is no w/h specified for the svg * fix(leadingIcon styles): remove export --------- Co-authored-by: Carlos Cortizas <[email protected]>
- Loading branch information
1 parent
adaa8dc
commit 82b5db8
Showing
13 changed files
with
260 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@commercetools-uikit/icons': minor | ||
--- | ||
|
||
Adds new CustomIcon component for displaying non-ui-kit-svgs. |
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,4 @@ | ||
{ | ||
"main": "dist/commercetools-uikit-icons-custom-icon.cjs.js", | ||
"module": "dist/commercetools-uikit-icons-custom-icon.esm.js" | ||
} |
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
40 changes: 40 additions & 0 deletions
40
packages/components/icons/src/custom-icon/custom-icon.spec.tsx
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,40 @@ | ||
import { ArrowLeftIcon } from '../generated'; | ||
import { screen, render } from '../../../../../test/test-utils'; | ||
import rawSvg from '../fixtures/raw-svg'; | ||
import CustomIcon, { type TCustomIconProps } from './custom-icon'; | ||
|
||
type TCustomIconTestProps = Pick< | ||
TCustomIconProps, | ||
'size' | 'icon' | 'hasBorder' | ||
> & { | ||
'data-testid'?: string; | ||
'aria-label': string; | ||
}; | ||
|
||
const createTestProps = ( | ||
custom?: TCustomIconTestProps | ||
): TCustomIconTestProps => ({ | ||
size: '20', | ||
icon: <ArrowLeftIcon aria-label="arrowLeft" />, | ||
'aria-label': 'custom-icon-test', | ||
...custom, | ||
}); | ||
|
||
describe('CustomIcon', () => { | ||
let props: TCustomIconTestProps; | ||
beforeEach(() => { | ||
props = createTestProps(); | ||
}); | ||
it('should render a react component and pass aria attributes', async () => { | ||
render(<CustomIcon {...props} />); | ||
await screen.findByRole('img', { name: 'custom-icon-test' }); | ||
}); | ||
it('should pass data attributes', async () => { | ||
render(<CustomIcon {...props} data-testid="test-testid" />); | ||
await screen.findByTestId('test-testid'); | ||
}); | ||
it('should render a custom svg when svg prop is passed', async () => { | ||
render(<CustomIcon icon={rawSvg.clock} />); | ||
await screen.findByLabelText('custom clock svg'); | ||
}); | ||
}); |
30 changes: 30 additions & 0 deletions
30
packages/components/icons/src/custom-icon/custom-icon.styles.ts
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,30 @@ | ||
import { designTokens } from '@commercetools-uikit/design-system'; | ||
import { css } from '@emotion/react'; | ||
import { type TCustomIconProps } from './custom-icon'; | ||
|
||
const sizeMap = { | ||
10: designTokens.spacing50, | ||
20: `calc(${designTokens.spacing50} + ${designTokens.spacing20})`, | ||
30: designTokens.spacing60, | ||
40: designTokens.spacing70, | ||
}; | ||
|
||
export const getCustomIconStyles = (props: TCustomIconProps) => { | ||
const sizeStyles = { | ||
height: sizeMap[props.size!], | ||
width: sizeMap[props.size!], | ||
}; | ||
|
||
return css` | ||
display: inline-block; | ||
height: ${sizeStyles.height}; | ||
width: ${sizeStyles.width}; | ||
border-radius: ${designTokens.borderRadius4}; | ||
background-color: ${designTokens.colorTransparent}; | ||
box-sizing: border-box; | ||
overflow: hidden; | ||
border: ${props.hasBorder | ||
? `solid ${designTokens.borderWidth1} ${designTokens.colorNeutral90}` | ||
: 'none'}; | ||
`; | ||
}; |
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,47 @@ | ||
import { type ReactElement, cloneElement } from 'react'; | ||
import { | ||
filterAriaAttributes, | ||
filterDataAttributes, | ||
} from '@commercetools-uikit/utils'; | ||
import InlineSvg from '../inline-svg/inline-svg'; | ||
import { getCustomIconStyles } from './custom-icon.styles'; | ||
|
||
export type TCustomIconProps = { | ||
/** | ||
* Indicates the size of the component | ||
*/ | ||
size?: '10' | '20' | '30' | '40'; | ||
/** | ||
* Indicates whether the component should display a border | ||
*/ | ||
hasBorder?: boolean; | ||
/** | ||
* An <Icon /> component, must pass either an icon prop or an svg prop | ||
*/ | ||
icon: ReactElement | string; | ||
}; | ||
|
||
const defaultProps: Required<Pick<TCustomIconProps, 'size' | 'hasBorder'>> = { | ||
size: '20', | ||
hasBorder: true, | ||
}; | ||
|
||
const CustomIcon = (props: TCustomIconProps) => ( | ||
<div | ||
role="img" | ||
css={getCustomIconStyles(props)} | ||
{...filterDataAttributes(props)} | ||
{...filterAriaAttributes(props)} | ||
> | ||
{typeof props.icon === 'string' ? ( | ||
<InlineSvg data={props.icon} size={'scale'} /> | ||
) : ( | ||
cloneElement(props.icon) | ||
)} | ||
</div> | ||
); | ||
|
||
CustomIcon.displayName = 'CustomIcon'; | ||
CustomIcon.defaultProps = defaultProps; | ||
|
||
export default CustomIcon; |
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 @@ | ||
export type { TCustomIconProps } from './custom-icon'; |
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,3 @@ | ||
export { default } from './custom-icon'; | ||
|
||
export * from './export-types'; |
30 changes: 30 additions & 0 deletions
30
packages/components/icons/src/fixtures/CustomIconReact.tsx
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,30 @@ | ||
const SvgCustomIcon = () => ( | ||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 35 35" role="img"> | ||
<g clipPath="url(#custom-icon_react_svg__a)"> | ||
<path fill="#fff" d="M0 0h35v35H0z" /> | ||
<path | ||
fill="#6359FF" | ||
fillRule="evenodd" | ||
d="M16.059 20q-.45 0-.755-.305a1.02 1.02 0 0 1-.304-.754V3.06q0-.45.304-.755.305-.304.755-.304h7.597q.37 0 .662.238.29.239.37.61l.265 1.27h4.87q.45-.001.754.304.305.304.305.754v8.471q0 .45-.305.754a1.02 1.02 0 0 1-.754.305h-5.479q-.37 0-.662-.238a1.05 1.05 0 0 1-.37-.61l-.265-1.27h-5.93v6.353q0 .45-.303.754a1.02 1.02 0 0 1-.755.305" | ||
clipRule="evenodd" | ||
/> | ||
<path | ||
fill="#000" | ||
d="M9.554 22.481-1 37.5h37L22.45 21.833a8.19 8.19 0 0 0-12.896.648" | ||
/> | ||
<path | ||
stroke="#fff" | ||
strokeWidth={1.5} | ||
d="m5 36.5 13.66-4.673c1.782-.61 1.81-3.121.04-3.77l-5.252-1.926c-1.66-.61-1.772-2.916-.177-3.681L21.5 18.5" | ||
/> | ||
</g> | ||
<defs> | ||
<clipPath id="custom-icon_react_svg__a"> | ||
<path fill="#fff" d="M0 0h35v35H0z" /> | ||
</clipPath> | ||
</defs> | ||
</svg> | ||
); | ||
SvgCustomIcon.displayName = 'SvgCustomIcon'; | ||
|
||
export default SvgCustomIcon; |
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