-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! Feat(web-react): Add spacing property to Tabs component #DS-1315
- Loading branch information
Showing
4 changed files
with
66 additions
and
24 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
39 changes: 39 additions & 0 deletions
39
packages/web-react/src/hooks/__tests__/useSpacingStyles.test.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,39 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
import { ComponentSpacingProps } from '../../types/shared'; | ||
import { useSpacingStyle } from '../useSpacingStyle'; | ||
|
||
describe('useSpacingStyles', () => { | ||
it('should process spacing if spacing is an object', () => { | ||
const mockProps = { | ||
mobile: 'space-100', | ||
tablet: 'space-200', | ||
desktop: 'space-300', | ||
}; | ||
|
||
const { result } = renderHook(() => useSpacingStyle(mockProps as ComponentSpacingProps, 'test-prefix')); | ||
|
||
expect(result.current).toEqual({ | ||
'--test-prefix-spacing': 'var(--spirit-space-100)', | ||
'--test-prefix-spacing-tablet': 'var(--spirit-space-200)', | ||
'--test-prefix-spacing-desktop': 'var(--spirit-space-300)', | ||
}); | ||
}); | ||
|
||
it('should process if spacing is a string', () => { | ||
const mockProps = 'space-100'; | ||
|
||
const { result } = renderHook(() => useSpacingStyle(mockProps as ComponentSpacingProps, 'test-prefix')); | ||
|
||
expect(result.current).toEqual({ | ||
'--test-prefix-spacing': 'var(--spirit-space-100)', | ||
}); | ||
}); | ||
|
||
it('should process if spacing is undefined', () => { | ||
const mockProps = undefined; | ||
|
||
const { result } = renderHook(() => useSpacingStyle(mockProps as ComponentSpacingProps | undefined, 'test-prefix')); | ||
|
||
expect(result.current).toEqual({}); | ||
}); | ||
}); |
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,23 @@ | ||
import { CSSProperties } from 'react'; | ||
import { ComponentSpacingProps } from '../types'; | ||
|
||
interface SpacingCSSProperties extends CSSProperties { | ||
[index: `--${string}`]: string | undefined | number; | ||
} | ||
|
||
export function useSpacingStyle(spacing: ComponentSpacingProps | undefined, prefix: string): SpacingCSSProperties { | ||
const style: SpacingCSSProperties = {}; | ||
|
||
if (typeof spacing === 'object' && spacing !== null) { | ||
Object.keys(spacing).forEach((key) => { | ||
const suffix = key === 'mobile' ? '' : `-${key}`; | ||
(style as Record<string, string | undefined>)[`--${prefix}-spacing${suffix}`] = `var(--spirit-${spacing[ | ||
key as keyof typeof spacing | ||
]?.toString()})`; | ||
}); | ||
} else if (spacing) { | ||
(style as Record<string, string | undefined>)[`--${prefix}-spacing`] = `var(--spirit-${spacing})`; | ||
} | ||
|
||
return style; | ||
} |