diff --git a/packages/block-editor/CHANGELOG.md b/packages/block-editor/CHANGELOG.md index 2ecbb9f6e426d2..6213d299c3e5e0 100644 --- a/packages/block-editor/CHANGELOG.md +++ b/packages/block-editor/CHANGELOG.md @@ -79,6 +79,7 @@ - Convert `Iframe`, `EditorStyles`, `NavigableToolbar`, `CopyHandler` and `BlockBreadcrumb` to TypeScript ([#84922](https://github.com/WordPress/gutenberg/pull/84922)). - Convert `MediaPlaceholder` and `ImageEditor` to TypeScript ([#84923](https://github.com/WordPress/gutenberg/pull/84923)). - Convert `useBlockDropZone` to TypeScript ([#84946](https://github.com/WordPress/gutenberg/pull/84946)). +- Convert `BlockManager`, `PublishDateTimePicker` and the fit text hook to TypeScript ([#84949](https://github.com/WordPress/gutenberg/pull/84949)). ## 18.1.0 (2026-10-07) diff --git a/packages/block-editor/src/components/block-manager/category.jsx b/packages/block-editor/src/components/block-manager/category.tsx similarity index 84% rename from packages/block-editor/src/components/block-manager/category.jsx rename to packages/block-editor/src/components/block-manager/category.tsx index 3cc89ab6bc9c04..38df1b5d5f85f1 100644 --- a/packages/block-editor/src/components/block-manager/category.jsx +++ b/packages/block-editor/src/components/block-manager/category.tsx @@ -1,18 +1,20 @@ import { useCallback } from '@wordpress/element'; import { useInstanceId } from '@wordpress/compose'; import { CheckboxControl as WCCheckboxControl } from '@wordpress/components'; +import type { BlockType } from '@wordpress/blocks'; import BlockTypesChecklist from './checklist'; +import type { BlockManagerCategoryProps } from './types'; function BlockManagerCategory( { title, blockTypes, selectedBlockTypes, onChange, -} ) { +}: BlockManagerCategoryProps ) { const instanceId = useInstanceId( BlockManagerCategory ); const toggleVisible = useCallback( - ( blockType, nextIsChecked ) => { + ( blockType: BlockType, nextIsChecked: boolean ) => { if ( nextIsChecked ) { onChange( [ ...selectedBlockTypes, blockType ] ); } else { @@ -27,7 +29,7 @@ function BlockManagerCategory( { ); const toggleAllVisible = useCallback( - ( nextIsChecked ) => { + ( nextIsChecked: boolean ) => { if ( nextIsChecked ) { onChange( [ ...selectedBlockTypes, @@ -80,7 +82,12 @@ function BlockManagerCategory( { onChange={ toggleAllVisible } className="block-editor-block-manager__category-title" indeterminate={ isIndeterminate } - label={ { title } } + // The label renders as a node, so the title can carry its ID. + label={ + ( + { title } + ) as unknown as string + } /> { blockTypes.map( ( blockType ) => ( diff --git a/packages/block-editor/src/components/block-manager/index.jsx b/packages/block-editor/src/components/block-manager/index.tsx similarity index 86% rename from packages/block-editor/src/components/block-manager/index.jsx rename to packages/block-editor/src/components/block-manager/index.tsx index 070aecd4e219d6..41870af522fb34 100644 --- a/packages/block-editor/src/components/block-manager/index.jsx +++ b/packages/block-editor/src/components/block-manager/index.tsx @@ -10,22 +10,23 @@ import { useEffect, useState } from '@wordpress/element'; import { useDebounce } from '@wordpress/compose'; import { speak } from '@wordpress/a11y'; import BlockManagerCategory from './category'; +import type { BlockManagerProps } from './types'; /** * Provides a list of blocks with checkboxes. * - * @param {Object} props Props. - * @param {Array} props.blockTypes An array of blocks. - * @param {Array} props.selectedBlockTypes An array of selected blocks. - * @param {Function} props.onChange Function to be called when the selected blocks change. - * @param {boolean} props.showSelectAll Whether to show the select all checkbox. + * @param props Props. + * @param props.blockTypes An array of blocks. + * @param props.selectedBlockTypes An array of selected blocks. + * @param props.onChange Function to be called when the selected blocks change. + * @param props.showSelectAll Whether to show the select all checkbox. */ export default function BlockManager( { blockTypes, selectedBlockTypes, onChange, showSelectAll = true, -} ) { +}: BlockManagerProps ) { const debouncedSpeak = useDebounce( speak, 500 ); const [ search, setSearch ] = useState( '' ); const { categories, isMatchingSearchTerm } = useSelect( ( select ) => { @@ -86,7 +87,8 @@ export default function BlockManager( { /> ) }
void; + /** + * Whether to show the select all checkbox. + */ + showSelectAll?: boolean; +} + +/** + * Props of the block types of one category in `BlockManager`. + */ +export interface BlockManagerCategoryProps { + title: string; + blockTypes: BlockType[]; + selectedBlockTypes: BlockType[]; + onChange: ( selectedBlockTypes: BlockType[] ) => void; +} + +/** + * Props of the checklist of block types in `BlockManager`. + */ +export interface BlockTypesChecklistProps { + blockTypes: BlockType[]; + /** + * The names of the checked block types. + */ + value: string[]; + onItemChange: ( blockType: BlockType, nextIsChecked: boolean ) => void; +} diff --git a/packages/block-editor/src/components/publish-date-time-picker/index.jsx b/packages/block-editor/src/components/publish-date-time-picker/index.tsx similarity index 74% rename from packages/block-editor/src/components/publish-date-time-picker/index.jsx rename to packages/block-editor/src/components/publish-date-time-picker/index.tsx index 7526892790bc76..362270f6e02caf 100644 --- a/packages/block-editor/src/components/publish-date-time-picker/index.jsx +++ b/packages/block-editor/src/components/publish-date-time-picker/index.tsx @@ -1,8 +1,13 @@ +import type { ForwardedRef } from 'react'; import { DateTimePicker, TimePicker } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { forwardRef } from '@wordpress/element'; import { getSettings } from '@wordpress/date'; import InspectorPopoverHeader from '../inspector-popover-header'; +import type { + PrivatePublishDateTimePickerProps, + PublishDateTimePickerProps, +} from './types'; export function PublishDateTimePicker( { @@ -14,8 +19,8 @@ export function PublishDateTimePicker( currentDate, title, ...additionalProps - }, - ref + }: PrivatePublishDateTimePickerProps, + ref: ForwardedRef< HTMLDivElement > ) { const datePickerProps = { startOfWeek: getSettings().l10n.startOfWeek, @@ -24,7 +29,10 @@ export function PublishDateTimePicker( currentTime: isCompact ? currentDate : undefined, ...additionalProps, }; - const DatePickerComponent = isCompact ? TimePicker : DateTimePicker; + // `TimePicker` ignores the date props. + const DatePickerComponent = ( + isCompact ? TimePicker : DateTimePicker + ) as typeof DateTimePicker; return (
{ showPopoverHeader && ( @@ -50,7 +58,10 @@ export function PublishDateTimePicker( export const PrivatePublishDateTimePicker = forwardRef( PublishDateTimePicker ); -function PublicPublishDateTimePicker( props, ref ) { +function PublicPublishDateTimePicker( + props: PublishDateTimePickerProps, + ref: ForwardedRef< HTMLDivElement > +) { return ( & { + /** + * The title displayed in the header of the popover. + */ + title?: string; + /** + * Called when the user presses the close button. + */ + onClose?: () => void; + /** + * Whether to render the popover header. + */ + showPopoverHeader?: boolean; +}; + +/** + * Props of the private `PublishDateTimePicker`. + */ +export type PrivatePublishDateTimePickerProps = PublishDateTimePickerProps & { + /** + * Whether to render the "Reset" action in the popover header. + */ + showPopoverHeaderActions?: boolean; + /** + * Whether to only pick the time. + */ + isCompact?: boolean; +}; diff --git a/packages/block-editor/src/hooks/fit-text.jsx b/packages/block-editor/src/hooks/fit-text.tsx similarity index 74% rename from packages/block-editor/src/hooks/fit-text.jsx rename to packages/block-editor/src/hooks/fit-text.tsx index fde41ff7ebac1c..30ef1c216436ff 100644 --- a/packages/block-editor/src/hooks/fit-text.jsx +++ b/packages/block-editor/src/hooks/fit-text.tsx @@ -1,5 +1,6 @@ import { addFilter } from '@wordpress/hooks'; import { hasBlockSupport } from '@wordpress/blocks'; +import type { BlockType } from '@wordpress/blocks'; import { useEffect, useCallback, useState } from '@wordpress/element'; import { useSelect } from '@wordpress/data'; import { __ } from '@wordpress/i18n'; @@ -8,13 +9,31 @@ import { __experimentalToolsPanelItem as ToolsPanelItem, } from '@wordpress/components'; import { createHigherOrderComponent } from '@wordpress/compose'; -const EMPTY_OBJECT = {}; +const EMPTY_OBJECT: FitTextBlockState = {}; const MIN_FONT_SIZE_FOR_WARNING = 12; import { optimizeFitText } from '../utils/fit-text-utils'; import { store as blockEditorStore } from '../store'; import { useBlockElement } from '../components/block-list/use-block-props/use-block-refs'; import InspectorControls from '../components/inspector-controls'; import FitTextSizeWarning from '../components/fit-text-size-warning'; +import type { BlockAttributes } from '../store/types'; +import type { + BlockEditHookProps, + BlockWrapperProps, + FitTextAttributes, + FitTextControlProps, + FitTextFontSizeProps, + UseFitTextProps, +} from './types'; + +/** + * The block state the fit text recalculation depends on. + */ +interface FitTextBlockState { + blockAttributes?: BlockAttributes | null; + parentId?: string | null; + blockMode?: string; +} export const FIT_TEXT_SUPPORT_KEY = 'typography.fitText'; @@ -22,10 +41,10 @@ export const FIT_TEXT_SUPPORT_KEY = 'typography.fitText'; * Filters registered block settings, extending attributes to include * the `fitText` attribute. * - * @param {Object} settings Original block settings. - * @return {Object} Filtered block settings. + * @param settings Original block settings. + * @return Filtered block settings. */ -function addAttributes( settings ) { +function addAttributes( settings: BlockType ): BlockType { if ( ! hasBlockSupport( settings, FIT_TEXT_SUPPORT_KEY ) ) { return settings; } @@ -50,21 +69,25 @@ function addAttributes( settings ) { /** * Custom hook to handle fit text functionality in the editor. * - * @param {Object} props Component props. - * @param {?boolean} props.fitText Fit text attribute. - * @param {string} props.name Block name. - * @param {string} props.clientId Block client ID. + * @param props Component props. + * @param props.fitText Fit text attribute. + * @param props.name Block name. + * @param props.clientId Block client ID. */ -function useFitText( { fitText, name, clientId } ) { - const [ fontSize, setFontSize ] = useState( null ); +function useFitText( { fitText, name, clientId }: UseFitTextProps ) { + const [ fontSize, setFontSize ] = useState< number | null | undefined >( + null + ); const hasFitTextSupport = hasBlockSupport( name, FIT_TEXT_SUPPORT_KEY ); - const blockElement = useBlockElement( clientId ); + // Block elements are HTML elements. + const blockElement = useBlockElement( clientId ) as + HTMLElement | null | undefined; // Monitor block attribute changes, parent changes, and block mode. // Any attribute or parent change may change the available space. // Block mode is needed to disable fit text when in HTML editing mode. const { blockAttributes, parentId, blockMode } = useSelect( - ( select ) => { + ( select ): FitTextBlockState => { if ( ! clientId || ! hasFitTextSupport || ! fitText ) { return EMPTY_OBJECT; } @@ -92,7 +115,7 @@ function useFitText( { fitText, name, clientId } ) { // Size the measured element itself: a block rendered once per item in // a loop shares its client ID, so a rule keyed to it would size every // copy at once. Mirrors the front end in `fit-text-frontend.ts`. - const applyFontSizeStyle = ( size ) => { + const applyFontSizeStyle = ( size: number ) => { if ( size === 0 ) { blockElement.style.removeProperty( 'font-size' ); } else { @@ -120,9 +143,9 @@ function useFitText( { fitText, name, clientId } ) { const previousVisibility = currentElement.style.visibility; // Store IDs for cleanup - let hideFrameId = null; - let calculateFrameId = null; - let showTimeoutId = null; + let hideFrameId: number | null = null; + let calculateFrameId: number | null = null; + let showTimeoutId: ReturnType< typeof setTimeout > | null = null; // We are hiding the element doing the calculation of fit text // and then showing it again to avoid the user noticing a flash of potentially @@ -143,7 +166,7 @@ function useFitText( { fitText, name, clientId } ) { } ); // Watch for size changes - let resizeObserver; + let resizeObserver: ResizeObserver | undefined; if ( window.ResizeObserver && currentElement.parentElement ) { resizeObserver = new window.ResizeObserver( applyFitText ); resizeObserver.observe( currentElement.parentElement ); @@ -196,6 +219,7 @@ function useFitText( { fitText, name, clientId } ) { return () => window.cancelAnimationFrame( frameId ); } + return undefined; }, [ blockAttributes, fitText, @@ -211,13 +235,13 @@ function useFitText( { fitText, name, clientId } ) { /** * Fit text control component for the typography panel. * - * @param {Object} props Component props. - * @param {string} props.clientId Block client ID. - * @param {Function} props.setAttributes Function to set block attributes. - * @param {string} props.name Block name. - * @param {boolean} props.fitText Whether fit text is enabled. - * @param {string} props.fontSize Font size slug. - * @param {Object} props.style Block style object. + * @param props Component props. + * @param props.clientId Block client ID. + * @param props.setAttributes Function to set block attributes. + * @param props.name Block name. + * @param props.fitText Whether fit text is enabled. + * @param props.fontSize Font size slug. + * @param props.style Block style object. */ export function FitTextControl( { clientId, @@ -226,7 +250,7 @@ export function FitTextControl( { name, fontSize, style, -} ) { +}: FitTextControlProps ) { const hasSelectedBlockStyleState = useSelect( ( select ) => { const { @@ -259,7 +283,7 @@ export function FitTextControl( { checked={ fitText } onChange={ () => { const newFitText = ! fitText || undefined; - const updates = { fitText: newFitText }; + const updates: FitTextAttributes = { fitText: newFitText }; // When enabling fit text, clear font size if it has a value if ( newFitText ) { @@ -294,12 +318,16 @@ export function FitTextControl( { /** * Override props applied to the block element on save. * - * @param {Object} props Additional props applied to the block element. - * @param {Object} blockType Block type. - * @param {Object} attributes Block attributes. - * @return {Object} Filtered props applied to the block element. + * @param props Additional props applied to the block element. + * @param blockType Block type. + * @param attributes Block attributes. + * @return Filtered props applied to the block element. */ -function addSaveProps( props, blockType, attributes ) { +function addSaveProps( + props: BlockWrapperProps, + blockType: BlockType, + attributes: FitTextAttributes +) { if ( ! hasBlockSupport( blockType, FIT_TEXT_SUPPORT_KEY ) ) { return props; } @@ -324,12 +352,15 @@ function addSaveProps( props, blockType, attributes ) { /** * Override props applied to the block element in the editor. * - * @param {Object} props Component props including block attributes. - * @param {string} props.name Block name. - * @param {boolean} props.fitText Whether fit text is enabled. - * @return {Object} Filtered props applied to the block element. + * @param props Component props including block attributes. + * @param props.name Block name. + * @param props.fitText Whether fit text is enabled. + * @return Filtered props applied to the block element. */ -function useBlockProps( { name, fitText } ) { +function useBlockProps( { + name, + fitText, +}: Pick< UseFitTextProps, 'name' | 'fitText' > ) { if ( fitText && hasBlockSupport( name, FIT_TEXT_SUPPORT_KEY ) ) { return { className: 'has-fit-text', @@ -344,7 +375,7 @@ addFilter( addAttributes ); -const hasFitTextSupport = ( blockNameOrType ) => { +const hasFitTextSupport = ( blockNameOrType: string | BlockType ) => { return hasBlockSupport( blockNameOrType, FIT_TEXT_SUPPORT_KEY ); }; @@ -352,14 +383,19 @@ const hasFitTextSupport = ( blockNameOrType ) => { * Component that handles the fit text font size and displays a warning * if the fit text font size is below a minimum threshold. * - * @param {Object} props Component props. - * @param {boolean} props.fitText Whether fit text is enabled. - * @param {string} props.name Block name. - * @param {string} props.clientId Block client ID. - * @param {boolean} props.isSelected Whether the block is selected. - * @return {Element|null} Warning component or null. + * @param props Component props. + * @param props.fitText Whether fit text is enabled. + * @param props.name Block name. + * @param props.clientId Block client ID. + * @param props.isSelected Whether the block is selected. + * @return Warning component or null. */ -function FitTextFontSize( { fitText, name, clientId, isSelected } ) { +function FitTextFontSize( { + fitText, + name, + clientId, + isSelected, +}: FitTextFontSizeProps ) { const { fontSize } = useFitText( { fitText, name, clientId } ); if ( isSelected && fontSize && fontSize < MIN_FONT_SIZE_FOR_WARNING ) { return ( @@ -378,9 +414,10 @@ function FitTextFontSize( { fitText, name, clientId, isSelected } ) { * `editor.BlockEdit` filter. */ const addFitTextControl = createHigherOrderComponent( ( BlockEdit ) => { - return function AddFitTextControl( props ) { + return function AddFitTextControl( props: BlockEditHookProps ) { const { name, attributes, clientId, isSelected, setAttributes } = props; - const { fitText } = attributes; + // The block attributes include the fit text attributes. + const { fitText } = attributes as FitTextAttributes; const supportsFitText = hasBlockSupport( name, FIT_TEXT_SUPPORT_KEY ); if ( ! supportsFitText ) { return ; @@ -394,8 +431,10 @@ const addFitTextControl = createHigherOrderComponent( ( BlockEdit ) => { fitText={ fitText } setAttributes={ setAttributes } name={ name } - fontSize={ attributes.fontSize } - style={ attributes.style } + fontSize={ + ( attributes as FitTextAttributes ).fontSize + } + style={ ( attributes as FitTextAttributes ).style } /> { fitText && ( diff --git a/packages/block-editor/src/hooks/types.ts b/packages/block-editor/src/hooks/types.ts index 7db8a88641567f..0af0c5307b5b7c 100644 --- a/packages/block-editor/src/hooks/types.ts +++ b/packages/block-editor/src/hooks/types.ts @@ -421,3 +421,58 @@ export interface PositionCSSOptions { export interface ResponsivePositionCSSOptions extends PositionCSSOptions { viewportSettings?: unknown; } + +/** + * The attributes the fit text support reads. + */ +export type FitTextAttributes = { + fitText?: boolean; + fontSize?: string; + style?: BlockStyle & { typography?: BlockStyle }; +}; + +/** + * Props of the hook sizing a block's text to fit its container. + */ +export interface UseFitTextProps { + /** + * Fit text attribute. + */ + fitText?: boolean; + /** + * Block name. + */ + name: string; + /** + * Block client ID. + */ + clientId: string; +} + +/** + * Props of `FitTextControl`. + */ +export interface FitTextControlProps extends FitTextAttributes { + /** + * Block client ID. + */ + clientId: string; + /** + * Function to set block attributes. + */ + setAttributes: ( attributes: Record< string, unknown > ) => void; + /** + * Block name. + */ + name: string; +} + +/** + * Props of the component warning about a small fit text font size. + */ +export interface FitTextFontSizeProps extends UseFitTextProps { + /** + * Whether the block is selected. + */ + isSelected?: boolean; +} diff --git a/tools/eslint/suppressions.json b/tools/eslint/suppressions.json index 972743c18c0ed6..b399a83c272d74 100644 --- a/tools/eslint/suppressions.json +++ b/tools/eslint/suppressions.json @@ -410,37 +410,19 @@ "count": 2 } }, - "packages/block-editor/src/components/block-manager/category.jsx": { + "packages/block-editor/src/components/block-manager/category.tsx": { "@wordpress/use-recommended-components": { "count": 1 - }, - "gutenberg/use-typescript": { - "count": 1 - }, - "react/jsx-filename-extension": { - "count": 1 } }, - "packages/block-editor/src/components/block-manager/checklist.jsx": { + "packages/block-editor/src/components/block-manager/checklist.tsx": { "@wordpress/use-recommended-components": { "count": 1 - }, - "gutenberg/use-typescript": { - "count": 1 - }, - "react/jsx-filename-extension": { - "count": 1 } }, - "packages/block-editor/src/components/block-manager/index.jsx": { + "packages/block-editor/src/components/block-manager/index.tsx": { "@wordpress/use-recommended-components": { "count": 2 - }, - "gutenberg/use-typescript": { - "count": 1 - }, - "react/jsx-filename-extension": { - "count": 1 } }, "packages/block-editor/src/components/block-mover/button.jsx": { @@ -2019,14 +2001,6 @@ "count": 1 } }, - "packages/block-editor/src/components/publish-date-time-picker/index.jsx": { - "gutenberg/use-typescript": { - "count": 1 - }, - "react/jsx-filename-extension": { - "count": 1 - } - }, "packages/block-editor/src/components/recursion-provider/index.jsx": { "gutenberg/use-typescript": { "count": 1 @@ -2357,15 +2331,9 @@ "count": 1 } }, - "packages/block-editor/src/hooks/fit-text.jsx": { + "packages/block-editor/src/hooks/fit-text.tsx": { "@wordpress/use-recommended-components": { "count": 1 - }, - "gutenberg/use-typescript": { - "count": 1 - }, - "react/jsx-filename-extension": { - "count": 1 } }, "packages/block-editor/src/hooks/font-family.js": {