Skip to content

Commit d503b09

Browse files
author
Jannik Weise
committed
🎨 Enforces Theme type for styled-components useTheme hook
Ensures that the `useTheme` hook is correctly typed as `Theme` from the `ColorSchemeProvider` across various components. This provides type safety when accessing theme properties, preventing potential runtime errors. Specifically, the changes involve casting the result of `useTheme()` to `Theme` in components such as `AccordionHead`, `Button`, `SearchBox`, `SearchInput`, and `Slider`.
1 parent a08a7a6 commit d503b09

File tree

7 files changed

+16
-13
lines changed

7 files changed

+16
-13
lines changed

packages/core/src/components/accordion/accordion-head/AccordionHead.tsx

+7-5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { useTheme } from 'styled-components';
1515
import { useElementSize } from '../../../hooks/useElementSize';
1616
import { getAccordionHeadHeight, getElementClickEvent } from '../../../utils/accordion';
1717
import { AreaContext } from '../../area-provider/AreaContextProvider';
18+
import type { Theme } from '../../color-scheme-provider/ColorSchemeProvider';
1819
import Icon from '../../icon/Icon';
1920
import Input, { InputSize, type InputProps } from '../../input/Input';
2021
import SearchInput from '../../search-input/SearchInput';
@@ -82,7 +83,7 @@ const AccordionHead: FC<AccordionHeadProps> = ({
8283

8384
const [isSearchActive, setIsSearchActive] = useState(false);
8485

85-
const theme = useTheme();
86+
const theme = useTheme() as Theme;
8687

8788
const titleElementWrapperRef = useRef<HTMLDivElement>(null);
8889
const titleWrapperRef = useRef<HTMLDivElement>(null);
@@ -139,13 +140,13 @@ const AccordionHead: FC<AccordionHeadProps> = ({
139140

140141
if (
141142
theme?.accordionIcon &&
142-
theme.accordionIcon !== 110 &&
143-
theme.accordionIcon !== 1110100
143+
(theme.accordionIcon as unknown as number) !== 110 &&
144+
(theme.accordionIcon as unknown as number) !== 1110100
144145
) {
145-
internalIcon = (theme.accordionIcon as number).toString(16);
146+
internalIcon = (theme.accordionIcon as unknown as number).toString(16);
146147
}
147148

148-
const internalIconStyle = theme?.iconStyle ? (theme.iconStyle as string) : 'fa-regular';
149+
const internalIconStyle = theme?.iconStyle ? theme.iconStyle : 'fa-regular';
149150

150151
return <StyledAccordionIcon className={internalIconStyle} $icon={internalIcon} />;
151152
}, [icon, theme, isFixed]);
@@ -184,6 +185,7 @@ const AccordionHead: FC<AccordionHeadProps> = ({
184185
{typeof onTitleInputChange === 'function' ? (
185186
// eslint-disable-next-line react/jsx-no-constructed-context-values
186187
<AreaContext.Provider value={{ shouldChangeColor: true }}>
188+
{/* eslint-disable-next-line react/jsx-props-no-spreading */}
187189
<Input {...titleInputProps} value={title} onChange={onTitleInputChange} />
188190
</AreaContext.Provider>
189191
) : (

packages/core/src/components/button/Button.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ const Button: FC<ButtonProps> = ({
7777

7878
const buttonClasses = clsx('beta-chayns-button ellipsis', className);
7979

80-
const theme: Theme = useTheme();
80+
const theme = useTheme() as Theme;
8181

8282
const iconColor = useMemo(() => {
8383
if (isSecondary) {

packages/core/src/components/icon/Icon.tsx

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import clsx from 'clsx';
22
import React, { FC, MouseEventHandler } from 'react';
3-
import { useTheme } from 'styled-components';
43
import { getStackSizeFactor } from '../../utils/icon';
54
import { StyledIcon, StyledIconWrapper } from './Icon.styles';
65

@@ -74,8 +73,6 @@ const Icon: FC<IconProps> = ({
7473
}
7574
};
7675

77-
const theme = useTheme();
78-
7976
let maxStackSizeFactor = 1;
8077

8178
icons.forEach((icon) => {

packages/core/src/components/search-box/SearchBox.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import type { IFilterButtonItem } from '../../types/filterButtons';
2424
import type { ISearchBoxItem, ISearchBoxItems } from '../../types/searchBox';
2525
import { calculateContentHeight } from '../../utils/calculate';
2626
import { searchList } from '../../utils/searchBox';
27+
import type { Theme } from '../color-scheme-provider/ColorSchemeProvider';
2728
import type { ContextMenuCoordinates } from '../context-menu/ContextMenu';
2829
import Icon from '../icon/Icon';
2930
import Input from '../input/Input';
@@ -168,7 +169,7 @@ const SearchBox: FC<SearchBoxProps> = forwardRef<SearchBoxRef, SearchBoxProps>(
168169
typeof presetValue === 'string' && presetValue !== '',
169170
);
170171

171-
const theme = useTheme();
172+
const theme = useTheme() as Theme;
172173

173174
const { browser } = getDevice();
174175

packages/core/src/components/search-input/SearchInput.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
StyledSearchInputPseudoElement,
2020
} from './SearchInput.styles';
2121
import { useTheme } from 'styled-components';
22-
import { Theme } from '../color-scheme-provider/ColorSchemeProvider';
22+
import type { Theme } from '../color-scheme-provider/ColorSchemeProvider';
2323
import { useElementSize } from '../../hooks/useElementSize';
2424

2525
export type SearchInputProps = {

packages/core/src/components/slider/Slider.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
fillSlider,
99
getThumbMaxWidth,
1010
} from '../../utils/slider';
11+
import type { Theme } from '../color-scheme-provider/ColorSchemeProvider';
1112
import {
1213
StyledSlider,
1314
StyledSliderInput,
@@ -90,7 +91,7 @@ const Slider: FC<SliderProps> = ({
9091

9192
const sliderWrapperSize = useElementSize(sliderWrapperRef);
9293

93-
const theme = useTheme();
94+
const theme = useTheme() as Theme;
9495

9596
useEffect(() => {
9697
if (shouldShowThumbLabel) {

packages/core/src/components/tag-input/TagInput.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
StyledTagInputTagWrapperText,
2323
} from './TagInput.styles';
2424
import { AreaContext } from '../area-provider/AreaContextProvider';
25+
import type { Theme } from '../color-scheme-provider/ColorSchemeProvider';
2526

2627
export type TagInputProps = {
2728
/**
@@ -52,9 +53,10 @@ const TagInput = forwardRef<TagInputRef, TagInputProps>(
5253
const [currentValue, setCurrentValue] = useState('');
5354
const [selectedId, setSelectedId] = useState<Tag['id']>();
5455

55-
const theme = useTheme();
5656
const areaProvider = useContext(AreaContext);
5757

58+
const theme = useTheme() as Theme;
59+
5860
useEffect(() => {
5961
if (tags) {
6062
setInternalTags(tags);

0 commit comments

Comments
 (0)