Skip to content

Commit ebf8ace

Browse files
feat(useAntTheme): exposed generated ant theme (#475)
* feat(useTheme): exposed generated ant theme * Use themeWithFallback to generate ant theme * Added useAntTheme hook * code review * Removed eslint disable from tests --------- Co-authored-by: Federico Maggi <7142570+fredmaggiowski@users.noreply.github.com>
1 parent 2d26a5d commit ebf8ace

File tree

5 files changed

+113
-4
lines changed

5 files changed

+113
-4
lines changed

src/hooks/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* SPDX-License-Identifier: Apache-2.0
1717
*/
1818

19+
import { useAntTheme } from './useAntTheme'
1920
import { useFeedbackMessage } from './useFeedbackMessage'
2021
import { useModal } from './useModal'
2122
import { useTheme } from './useTheme'
@@ -24,4 +25,5 @@ export default {
2425
useFeedbackMessage,
2526
useModal,
2627
useTheme,
28+
useAntTheme,
2729
}

src/hooks/useAntTheme/index.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Copyright 2024 Mia srl
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
*/
18+
19+
export { useAntTheme } from './useAntTheme'
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Copyright 2024 Mia srl
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
*/
18+
19+
import Theme from '../../themes/schema'
20+
import { ThemeProvider } from '../../components/ThemeProvider'
21+
import { ThemeProviderProps } from '../../components/ThemeProvider/ThemeProvider.props'
22+
import { generateAntTheme } from '../../components/ThemeProvider/Ant'
23+
import { renderHook } from '../../test-utils'
24+
import themes from '../../themes'
25+
import { useAntTheme } from './useAntTheme'
26+
27+
const { lightTheme } = themes
28+
29+
describe('useAntTheme', () => {
30+
const themeProvider = (defaultTheme: Theme) => function component({ theme, children }: ThemeProviderProps) {
31+
return (
32+
<ThemeProvider theme={theme ?? defaultTheme}>
33+
{children}
34+
</ThemeProvider>
35+
)
36+
}
37+
38+
test('correctly generates ant theme from default theme', () => {
39+
const antTheme = generateAntTheme(lightTheme)
40+
41+
const { result } = renderHook(() => useAntTheme(), { wrapper: ThemeProvider })
42+
43+
expect(result.current).toEqual(antTheme)
44+
})
45+
46+
for (const [themeName, theme] of Object.entries(themes)) {
47+
test(`correctly generates ant theme from ${themeName} theme`, () => {
48+
const antTheme = generateAntTheme(theme)
49+
50+
const { result } = renderHook(() => useAntTheme(), { wrapper: themeProvider(theme) })
51+
52+
expect(result.current).toEqual(antTheme)
53+
})
54+
}
55+
})

src/hooks/useAntTheme/useAntTheme.ts

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright 2024 Mia srl
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
*/
18+
19+
import { ThemeConfig } from 'antd'
20+
import { useMemo } from 'react'
21+
22+
import { generateAntTheme } from '../../components/ThemeProvider/Ant'
23+
import { useTheme } from '../useTheme'
24+
25+
/**
26+
* A hook to compute the Ant Design theme from the current theme.
27+
*
28+
* @link https://ant.design/docs/react/customize-theme#api
29+
*
30+
* @returns {ThemeConfig} The Ant Design theme.
31+
*/
32+
export const useAntTheme = (): ThemeConfig => {
33+
const theme = useTheme()
34+
35+
return useMemo(() => generateAntTheme(theme), [theme])
36+
}

src/hooks/useTheme/useTheme.test.tsx

+1-4
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
* SPDX-License-Identifier: Apache-2.0
1717
*/
1818

19-
/* eslint-disable func-names */
20-
/* eslint-disable react/display-name */
21-
2219
import Theme from '../../themes/schema'
2320
import { ThemeProvider } from '../../components/ThemeProvider'
2421
import { ThemeProviderProps } from '../../components/ThemeProvider/ThemeProvider.props'
@@ -29,7 +26,7 @@ import { useTheme } from './useTheme'
2926
const { lightTheme } = themes
3027

3128
describe('useTheme', () => {
32-
const themeProvider = (defaultTheme: Theme) => function({ theme, children }: ThemeProviderProps) {
29+
const themeProvider = (defaultTheme: Theme) => function component({ theme, children }: ThemeProviderProps) {
3330
return (
3431
<ThemeProvider theme={theme ?? defaultTheme}>
3532
{children}

0 commit comments

Comments
 (0)