|
1 | 1 | import "../css/AccordionStylesOverride.css"; |
2 | 2 |
|
3 | 3 | import * as React from "react"; |
4 | | - |
5 | 4 | import { DivAttributes } from "../helpers/types"; |
6 | 5 | import { Provider } from "./AccordionContext"; |
7 | 6 | import { UUID } from "./ItemContext"; |
| 7 | +import { IPartialTheme, ITheme } from 'office-ui-fabric-react/lib/Styling'; |
| 8 | +import { getFluentUIThemeOrDefault } from "../../../common/utilities/ThemeUtility"; |
| 9 | +import { useTheme } from "@fluentui/react-theme-provider/lib/useTheme"; |
| 10 | +import { useEffect, useRef } from "react"; |
8 | 11 |
|
9 | 12 | type AccordionProps = Pick< |
10 | | - DivAttributes, |
11 | | - Exclude<keyof DivAttributes, 'onChange'> |
12 | | -> & { |
| 13 | + DivAttributes, |
| 14 | + Exclude<keyof DivAttributes, 'onChange'>> & { |
13 | 15 | className?: string; |
14 | 16 | preExpanded?: UUID[]; |
15 | 17 | allowMultipleExpanded?: boolean; |
16 | 18 | allowZeroExpanded?: boolean; |
17 | 19 | onChange?(args: UUID[]): void; |
18 | | -}; |
| 20 | + /** |
| 21 | + * Set Fluent UI Theme. |
| 22 | + * If not set or set to null or not defined, the theme passed through context will be used, or the default theme of the page will be loaded. |
| 23 | + */ |
| 24 | + theme?: IPartialTheme | ITheme; |
| 25 | + }; |
19 | 26 |
|
20 | 27 | const Accordion = ({ |
21 | | - className = 'accordion', |
22 | | - allowMultipleExpanded, |
23 | | - allowZeroExpanded, |
24 | | - onChange, |
25 | | - preExpanded, |
26 | | - ...rest |
| 28 | + className = 'accordion', |
| 29 | + allowMultipleExpanded, |
| 30 | + allowZeroExpanded, |
| 31 | + onChange, |
| 32 | + preExpanded, |
| 33 | + theme, |
| 34 | + ...rest |
27 | 35 | }: AccordionProps): JSX.Element => { |
28 | | - return ( |
29 | | - <Provider |
30 | | - preExpanded={preExpanded} |
31 | | - allowMultipleExpanded={allowMultipleExpanded} |
32 | | - allowZeroExpanded={allowZeroExpanded} |
33 | | - onChange={onChange} |
34 | | - > |
35 | | - <div |
36 | | - data-accordion-component="Accordion" |
37 | | - className={className} |
38 | | - {...rest} |
39 | | - /> |
40 | | - </Provider> |
41 | | - ); |
| 36 | + |
| 37 | + const contextTheme = useTheme(); |
| 38 | + const divElement = useRef(null); |
| 39 | + |
| 40 | + useEffect(() => { |
| 41 | + if (divElement.current) { |
| 42 | + const themeToApply = getFluentUIThemeOrDefault((theme) ? theme : contextTheme); |
| 43 | + divElement.current.style.setProperty(`--accordion-bodyDivider`, themeToApply.semanticColors.bodyDivider); |
| 44 | + divElement.current.style.setProperty(`--accordion-buttonBackground`, themeToApply.semanticColors.buttonBackground); |
| 45 | + divElement.current.style.setProperty(`--accordion-buttonText`, themeToApply.semanticColors.buttonText); |
| 46 | + divElement.current.style.setProperty(`--accordion-buttonBackgroundHovered`, themeToApply.semanticColors.buttonBackgroundHovered); |
| 47 | + divElement.current.style.setProperty(`--accordion-bodyBackground`, themeToApply.semanticColors.bodyBackground); |
| 48 | + divElement.current.style.setProperty(`--accordion-bodyText`, themeToApply.semanticColors.bodyText); |
| 49 | + } |
| 50 | + }); |
| 51 | + |
| 52 | + return ( |
| 53 | + <Provider |
| 54 | + preExpanded={preExpanded} |
| 55 | + allowMultipleExpanded={allowMultipleExpanded} |
| 56 | + allowZeroExpanded={allowZeroExpanded} |
| 57 | + onChange={onChange} |
| 58 | + > |
| 59 | + <div |
| 60 | + data-accordion-component="Accordion" |
| 61 | + className={className} |
| 62 | + ref={divElement} |
| 63 | + {...rest} |
| 64 | + /> |
| 65 | + </Provider> |
| 66 | + ); |
42 | 67 | }; |
43 | 68 |
|
44 | 69 | export default Accordion; |
0 commit comments