diff --git a/packages/canon/.storybook/preview.tsx b/packages/canon/.storybook/preview.tsx index 8ffa2e55df..e278bb011f 100644 --- a/packages/canon/.storybook/preview.tsx +++ b/packages/canon/.storybook/preview.tsx @@ -1,7 +1,6 @@ import React from 'react'; import type { Preview, ReactRenderer } from '@storybook/react'; import { withThemeByDataAttribute } from '@storybook/addon-themes'; -import { CanonProvider } from '../src/contexts/canon'; // Canon specific styles import '../src/css/core.css'; @@ -87,11 +86,7 @@ const preview: Preview = { (element as HTMLElement).style.backgroundColor = 'var(--canon-bg)'; }); - return ( - - - - ); + return ; }, ], }; diff --git a/packages/canon/report.api.md b/packages/canon/report.api.md index f194084a9b..f36a53be6e 100644 --- a/packages/canon/report.api.md +++ b/packages/canon/report.api.md @@ -5,6 +5,7 @@ ```ts /// +import { Context } from 'react'; import type { CSSProperties } from 'react'; import { Field as Field_2 } from '@base-ui-components/react/field'; import { ForwardRefExoticComponent } from 'react'; @@ -162,23 +163,6 @@ export interface ButtonProps { variant?: ButtonOwnProps['variant']; } -// @public (undocumented) -export interface CanonContextProps { - // (undocumented) - icons: IconMap; -} - -// @public (undocumented) -export const CanonProvider: (props: CanonProviderProps) => React_2.JSX.Element; - -// @public (undocumented) -export interface CanonProviderProps { - // (undocumented) - children?: ReactNode; - // (undocumented) - overrides?: Partial>; -} - // @public (undocumented) export const Checkbox: React_2.ForwardRefExoticComponent< CheckboxProps & React_2.RefAttributes @@ -617,6 +601,15 @@ export type HeightProps = GetPropDefTypes; // @public (undocumented) export const Icon: (props: IconProps) => React_2.JSX.Element; +// @public (undocumented) +export const IconContext: Context; + +// @public (undocumented) +export interface IconContextProps { + // (undocumented) + icons: IconMap; +} + // @public (undocumented) export type IconMap = Partial>; @@ -651,6 +644,17 @@ export type IconProps = { style?: React.CSSProperties; }; +// @public (undocumented) +export const IconProvider: (props: IconProviderProps) => React_2.JSX.Element; + +// @public (undocumented) +export interface IconProviderProps { + // (undocumented) + children?: ReactNode; + // (undocumented) + overrides?: Partial>; +} + // @public (undocumented) export const icons: IconMap; @@ -968,7 +972,7 @@ export interface TextProps { } // @public (undocumented) -export const useCanon: () => CanonContextProps; +export const useIcons: () => IconContextProps; // @public (undocumented) export interface UtilityProps extends SpaceProps { diff --git a/packages/canon/src/components/Icon/Icon.stories.tsx b/packages/canon/src/components/Icon/Icon.stories.tsx index cdf63fbb32..15e7240bf3 100644 --- a/packages/canon/src/components/Icon/Icon.stories.tsx +++ b/packages/canon/src/components/Icon/Icon.stories.tsx @@ -17,7 +17,7 @@ import React from 'react'; import { Meta, StoryObj } from '@storybook/react'; import { Icon } from './Icon'; -import { CanonProvider } from '../../contexts/canon'; +import { IconProvider } from './provider'; import { icons } from './icons'; const meta = { @@ -52,9 +52,9 @@ export const WithCustomIcon: Story = { }, decorators: [ Story => ( -
Custom Icon
}}> +
Custom Icon
}}> -
+ ), ], }; diff --git a/packages/canon/src/components/Icon/Icon.tsx b/packages/canon/src/components/Icon/Icon.tsx index 4c7b55680a..ebb4d80d98 100644 --- a/packages/canon/src/components/Icon/Icon.tsx +++ b/packages/canon/src/components/Icon/Icon.tsx @@ -15,14 +15,14 @@ */ import React from 'react'; -import { useCanon } from '../../contexts/canon'; +import { useIcons } from './context'; import type { IconProps } from './types'; import clsx from 'clsx'; /** @public */ export const Icon = (props: IconProps) => { const { name, size, className, style, ...restProps } = props; - const { icons } = useCanon(); + const { icons } = useIcons(); const CanonIcon = icons[name] as React.ComponentType>; diff --git a/packages/canon/src/components/Icon/context.tsx b/packages/canon/src/components/Icon/context.tsx new file mode 100644 index 0000000000..4d1974d081 --- /dev/null +++ b/packages/canon/src/components/Icon/context.tsx @@ -0,0 +1,27 @@ +/* + * Copyright 2024 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { createContext, useContext } from 'react'; +import { icons } from './icons'; +import type { IconContextProps } from './types'; + +/** @public */ +export const IconContext = createContext({ + icons, +}); + +/** @public */ +export const useIcons = () => useContext(IconContext); diff --git a/packages/canon/src/components/Icon/index.tsx b/packages/canon/src/components/Icon/index.tsx index 0fa2eac35b..ac7dfdeea2 100644 --- a/packages/canon/src/components/Icon/index.tsx +++ b/packages/canon/src/components/Icon/index.tsx @@ -14,6 +14,15 @@ * limitations under the License. */ -export * from './Icon'; +// Icons export * from './icons'; + +// Icon component +export * from './Icon'; + +// Context and Provider +export * from './context'; +export * from './provider'; + +// Types export type * from './types'; diff --git a/packages/canon/src/contexts/canon.tsx b/packages/canon/src/components/Icon/provider.tsx similarity index 53% rename from packages/canon/src/contexts/canon.tsx rename to packages/canon/src/components/Icon/provider.tsx index 69a9e30b6f..0f296cfee4 100644 --- a/packages/canon/src/contexts/canon.tsx +++ b/packages/canon/src/components/Icon/provider.tsx @@ -14,38 +14,21 @@ * limitations under the License. */ -import React, { createContext, ReactNode, useContext } from 'react'; -import { icons } from '../components/Icon/icons'; -import { IconMap, IconNames } from '../components/Icon/types'; +import React from 'react'; +import { icons } from './icons'; +import { IconContext } from './context'; +import type { IconProviderProps } from './types'; /** @public */ -export interface CanonContextProps { - icons: IconMap; -} - -/** @public */ -export interface CanonProviderProps { - children?: ReactNode; - overrides?: Partial>; -} - -const CanonContext = createContext({ - icons, -}); - -/** @public */ -export const CanonProvider = (props: CanonProviderProps) => { +export const IconProvider = (props: IconProviderProps) => { const { children, overrides } = props; // Merge provided overrides with default icons const combinedIcons = { ...icons, ...overrides }; return ( - + {children} - + ); }; - -/** @public */ -export const useCanon = () => useContext(CanonContext); diff --git a/packages/canon/src/components/Icon/types.ts b/packages/canon/src/components/Icon/types.ts index 91fc6b9e60..d1402923c5 100644 --- a/packages/canon/src/components/Icon/types.ts +++ b/packages/canon/src/components/Icon/types.ts @@ -14,6 +14,8 @@ * limitations under the License. */ +import { ReactNode } from 'react'; + /** @public */ export type IconNames = | 'arrowDown' @@ -47,3 +49,14 @@ export type IconProps = { className?: string; style?: React.CSSProperties; }; + +/** @public */ +export interface IconContextProps { + icons: IconMap; +} + +/** @public */ +export interface IconProviderProps { + children?: ReactNode; + overrides?: Partial>; +} diff --git a/packages/canon/src/index.ts b/packages/canon/src/index.ts index 1168990966..18dad55e89 100644 --- a/packages/canon/src/index.ts +++ b/packages/canon/src/index.ts @@ -21,7 +21,7 @@ */ // Providers -export * from './contexts/canon'; +export * from './components/Icon/context'; // Layout components export * from './components/Box';