From a310d9c27a157f241cd5d8058fa1ca1668425bce Mon Sep 17 00:00:00 2001 From: Brian Ingles Date: Wed, 1 Nov 2023 14:50:23 -0500 Subject: [PATCH] Removed withChartTheme HOC #1572 --- .../chart/src/ChartThemeProviderUtils.tsx | 68 ------------------- packages/chart/src/index.ts | 2 +- packages/chart/src/useChartTheme.tsx | 15 ++++ 3 files changed, 16 insertions(+), 69 deletions(-) delete mode 100644 packages/chart/src/ChartThemeProviderUtils.tsx create mode 100644 packages/chart/src/useChartTheme.tsx diff --git a/packages/chart/src/ChartThemeProviderUtils.tsx b/packages/chart/src/ChartThemeProviderUtils.tsx deleted file mode 100644 index fb3d42d141..0000000000 --- a/packages/chart/src/ChartThemeProviderUtils.tsx +++ /dev/null @@ -1,68 +0,0 @@ -/* eslint-disable react/jsx-props-no-spreading */ -import { - ComponentType, - ForwardedRef, - forwardRef, - PropsWithoutRef, - RefAttributes, - useContext, -} from 'react'; -import { ChartTheme } from './ChartTheme'; - -import { - ChartThemeContext, - ChartThemeContextValue, -} from './ChartThemeProvider'; - -/** - * Hook to get the current `ChartThemeContextValue`. - */ -export function useChartTheme(): ChartThemeContextValue | null { - return useContext(ChartThemeContext); -} - -export type PropsWithChartTheme

= P & { - chartTheme: ChartTheme; -}; - -export type ComponentWithChartTheme< - P extends object, - T, -> = React.ForwardRefExoticComponent & RefAttributes> & { - WrappedComponent?: ComponentType>; -}; - -/** - * HOC for wrapping a given component in a `ChartThemeContext` provider. - * @param Component - */ -export function withChartTheme

( - Component: ComponentType> -): ComponentWithChartTheme { - /** - * Wrapper component that passes the current `ChartTheme` context value to the - * wrapped component. - * @param props Props to pass to the wrapped component - * @param ref Ref to pass to the wrapped component - */ - function WithChartTheme(props: P, ref: ForwardedRef): JSX.Element { - const chartTheme = useChartTheme(); - - if (chartTheme == null) { - throw new Error( - 'ChartTheme is null, did you forget to wrap your component in a ChartThemeProvider?' - ); - } - - return ; - } - - const WithChartThemeForwardRef: ComponentWithChartTheme = - forwardRef(WithChartTheme); - - // Mimicking Redux connect HOC api since some utils check for the existence - // of this property to identify a wrapped component - WithChartThemeForwardRef.WrappedComponent = Component; - - return WithChartThemeForwardRef; -} diff --git a/packages/chart/src/index.ts b/packages/chart/src/index.ts index 63f891db95..0959cc5d2f 100644 --- a/packages/chart/src/index.ts +++ b/packages/chart/src/index.ts @@ -8,5 +8,5 @@ export { default as MockChartModel } from './MockChartModel'; export { default as Plot } from './plotly/Plot'; export * from './ChartTheme'; export * from './ChartThemeProvider'; -export * from './ChartThemeProviderUtils'; export { default as isFigureChartModel } from './isFigureChartModel'; +export * from './useChartTheme'; diff --git a/packages/chart/src/useChartTheme.tsx b/packages/chart/src/useChartTheme.tsx new file mode 100644 index 0000000000..3646c8521e --- /dev/null +++ b/packages/chart/src/useChartTheme.tsx @@ -0,0 +1,15 @@ +import { useContext } from 'react'; + +import { + ChartThemeContext, + ChartThemeContextValue, +} from './ChartThemeProvider'; + +/** + * Hook to get the current `ChartThemeContextValue`. + */ +export function useChartTheme(): ChartThemeContextValue | null { + return useContext(ChartThemeContext); +} + +export default useChartTheme;