Skip to content

Commit cc6c141

Browse files
committed
Extended spacer to support theme sizes
1 parent b52a7cb commit cc6c141

File tree

3 files changed

+51
-18
lines changed

3 files changed

+51
-18
lines changed
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import React, { HTMLAttributes } from 'react'
22
import styled from 'styled-components'
3+
import { useTheme } from '@redis-ui/styles'
4+
import { Spacer } from '../spacer'
35

46
interface RiEmptyPromptProps extends Omit<HTMLAttributes<HTMLDivElement>, 'title'> {
57
body?: React.ReactNode
@@ -14,26 +16,26 @@ const StyledEmptyPrompt = styled.div`
1416
margin: auto;
1517
`
1618

17-
const Spacer = styled.div`
18-
height: ${({ theme }) => theme.core.space.space100};
19-
`
19+
const RiEmptyPrompt = ({ body, title, icon, ...rest }: RiEmptyPromptProps) => {
20+
const theme = useTheme()
2021

21-
const RiEmptyPrompt = ({ body, title, icon, ...rest }: RiEmptyPromptProps) => (
22-
<StyledEmptyPrompt {...rest}>
22+
return (<StyledEmptyPrompt {...rest}>
2323
{icon}
2424
{title && (
2525
<>
26-
<Spacer />
26+
<Spacer size={theme.core.space.space100} />
2727
{title}
2828
</>
2929
)}
3030
{body && (
3131
<>
32-
<Spacer />
32+
<Spacer size={theme.core.space.space100} />
3333
{body}
3434
</>
3535
)}
3636
</StyledEmptyPrompt>
37-
)
37+
)
38+
}
39+
3840

3941
export default RiEmptyPrompt

redisinsight/ui/src/components/base/layout/spacer/spacer.styles.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import { HTMLAttributes, ReactNode } from 'react'
22
import styled from 'styled-components'
33
import { CommonProps } from 'uiSrc/components/base/theme/types'
4+
import { theme } from 'uiSrc/components/base/theme'
45

56
export const SpacerSizes = ['xs', 's', 'm', 'l', 'xl', 'xxl'] as const
67
export type SpacerSize = (typeof SpacerSizes)[number]
8+
9+
// Extract only the spaceXXX keys from the theme
10+
export type ThemeSpacingKey = Extract<keyof typeof theme.semantic.core.space, `space${string}`>
11+
// Allow direct theme spacing values
12+
export type ThemeSpacingValue = typeof theme.semantic.core.space[ThemeSpacingKey]
13+
714
export type SpacerProps = CommonProps &
815
HTMLAttributes<HTMLDivElement> & {
916
children?: ReactNode
10-
size?: SpacerSize
17+
size?: SpacerSize | ThemeSpacingKey | ThemeSpacingValue
1118
}
1219

1320
export const spacerStyles = {
@@ -20,7 +27,26 @@ export const spacerStyles = {
2027
xxl: 'var(--size-xxl)',
2128
}
2229

30+
const isThemeSpacingKey = (
31+
size: SpacerSize | ThemeSpacingKey | ThemeSpacingValue
32+
): size is ThemeSpacingKey => typeof size === 'string' && size in theme.semantic.core.space
33+
34+
const getSpacingValue = (
35+
size: SpacerSize | ThemeSpacingKey | ThemeSpacingValue,
36+
): string => {
37+
const themeSpacingValues = Object.values(theme.semantic.core.space)
38+
if (typeof size === 'string' && themeSpacingValues.includes(size)) {
39+
return size
40+
}
41+
42+
if (isThemeSpacingKey(size)) {
43+
return theme?.semantic?.core?.space?.[size] || '0'
44+
}
45+
46+
return spacerStyles[size as SpacerSize]
47+
}
48+
2349
export const StyledSpacer = styled.div<SpacerProps>`
2450
flex-shrink: 0;
25-
height: ${({ size = 'l' }) => spacerStyles[size]};
51+
height: ${({ size = 'l' }) => getSpacingValue(size)};
2652
`
Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react'
22
import cx from 'classnames'
3+
import { useTheme } from '@redis-ui/styles'
34
import {
45
SpacerProps,
56
StyledSpacer,
@@ -9,17 +10,21 @@ import {
910
* A simple spacer component that can be used to add vertical spacing between
1011
* other components. The size of the spacer can be specified using the `size`
1112
* prop, which can be one of the following values:
12-
* - 'xs' = 4px
13-
* - 's' = 8px
14-
* - 'm' = 12px
15-
* - 'l' = 16px
16-
* - 'xl' = 24px
17-
* - 'xxl' = 32px.
13+
* - Legacy sizes: 'xs' = 4px, 's' = 8px, 'm' = 12px, 'l' = 16px, 'xl' = 24px, 'xxl' = 32px
14+
* - Theme spacing sizes: Any key from theme.semantic.core.space (e.g., 'space000', 'space010',
15+
* 'space025', 'space050', 'space100', 'space150', 'space200', 'space250', 'space300',
16+
* 'space400', 'space500', 'space550', 'space600', 'space800', etc.)
17+
*
18+
* The theme spacing tokens are dynamically extracted from the theme, ensuring consistency
19+
* and automatic updates when the theme changes.
1820
*
1921
* The default value for `size` is 'l'.
2022
*/
21-
export const Spacer = ({ className, children, ...rest }: SpacerProps) => (
22-
<StyledSpacer {...rest} className={cx('RI-spacer', className)}>
23+
export const Spacer = ({ className, children, ...rest }: SpacerProps) => {
24+
const theme = useTheme()
25+
return (
26+
<StyledSpacer {...rest} className={cx('RI-spacer', className)} theme={theme}>
2327
{children}
2428
</StyledSpacer>
2529
)
30+
}

0 commit comments

Comments
 (0)