Convert sidebar layout component to TypeScript (https://github.com/woocommerce/woocommerce-blocks/pull/7904)

This commit is contained in:
Tarun Vijwani 2023-01-05 11:56:05 +04:00 committed by GitHub
parent eebb3e41ea
commit bc116f002e
7 changed files with 92 additions and 71 deletions

View File

@ -1,18 +0,0 @@
/**
* External dependencies
*/
import { forwardRef } from 'react';
import classNames from 'classnames';
const Main = forwardRef( ( { children, className = '' }, ref ) => {
return (
<div
ref={ ref }
className={ classNames( 'wc-block-components-main', className ) }
>
{ children }
</div>
);
} );
export default Main;

View File

@ -0,0 +1,28 @@
/**
* External dependencies
*/
import { forwardRef } from 'react';
import classNames from 'classnames';
/**
* Internal dependencies
*/
import { ForwardRefProps } from './types';
const Main = forwardRef< HTMLInputElement, ForwardRefProps >(
( { children, className = '' }, ref ): JSX.Element => {
return (
<div
ref={ ref }
className={ classNames(
'wc-block-components-main',
className
) }
>
{ children }
</div>
);
}
);
export default Main;

View File

@ -2,15 +2,21 @@
* External dependencies
*/
import classNames from 'classnames';
import PropTypes from 'prop-types';
import { ContainerWidthContextProvider } from '@woocommerce/base-context';
/**
* Internal dependencies
*/
import './style.scss';
interface SidebarLayoutProps {
children: JSX.Element | JSX.Element[];
className: string;
}
const SidebarLayout = ( { children, className } ) => {
const SidebarLayout = ( {
children,
className,
}: SidebarLayoutProps ): JSX.Element => {
return (
<ContainerWidthContextProvider
className={ classNames(
@ -23,8 +29,4 @@ const SidebarLayout = ( { children, className } ) => {
);
};
SidebarLayout.propTypes = {
className: PropTypes.string,
};
export default SidebarLayout;

View File

@ -1,18 +0,0 @@
/**
* External dependencies
*/
import { forwardRef } from 'react';
import classNames from 'classnames';
const Sidebar = forwardRef( ( { children, className = '' }, ref ) => {
return (
<div
ref={ ref }
className={ classNames( 'wc-block-components-sidebar', className ) }
>
{ children }
</div>
);
} );
export default Sidebar;

View File

@ -0,0 +1,28 @@
/**
* External dependencies
*/
import { forwardRef } from 'react';
import classNames from 'classnames';
/**
* Internal dependencies
*/
import { ForwardRefProps } from './types';
const Sidebar = forwardRef< HTMLInputElement, ForwardRefProps >(
( { children, className = '' }, ref ): JSX.Element => {
return (
<div
ref={ ref }
className={ classNames(
'wc-block-components-sidebar',
className
) }
>
{ children }
</div>
);
}
);
export default Sidebar;

View File

@ -0,0 +1,4 @@
export type ForwardRefProps = {
children: JSX.Element | JSX.Element[];
className?: string;
};

View File

@ -1,44 +1,46 @@
/**
* External dependencies
*/
import PropTypes from 'prop-types';
import { createContext, useContext } from '@wordpress/element';
import { useContainerQueries } from '@woocommerce/base-hooks';
import classNames from 'classnames';
/**
* @typedef {import('@woocommerce/type-defs/contexts').ContainerWidthContext} ContainerWidthContext
* @typedef {import('react')} React
*/
export type ContainerWidthContextProps = {
hasContainerWidth: boolean;
containerClassName: string;
isMobile: boolean;
isSmall: boolean;
isMedium: boolean;
isLarge: boolean;
};
const ContainerWidthContext = createContext( {
hasContainerWidth: false,
containerClassName: '',
isMobile: false,
isSmall: false,
isMedium: false,
isLarge: false,
} );
const ContainerWidthContext: React.Context< ContainerWidthContextProps > =
createContext< ContainerWidthContextProps >( {
hasContainerWidth: false,
containerClassName: '',
isMobile: false,
isSmall: false,
isMedium: false,
isLarge: false,
} );
/**
* @return {ContainerWidthContext} Returns the container width context value
*/
export const useContainerWidthContext = () => {
export const useContainerWidthContext = (): ContainerWidthContextProps => {
return useContext( ContainerWidthContext );
};
interface ContainerWidthContextProviderProps {
children: JSX.Element | JSX.Element[];
className: string;
}
/**
* Provides an interface to useContainerQueries so children can see what size is being used by the
* container.
*
* @param {Object} props Incoming props for the component.
* @param {React.ReactChildren} props.children React elements wrapped by the component.
* @param {string} props.className CSS class in use.
*/
export const ContainerWidthContextProvider = ( {
children,
className = '',
} ) => {
}: ContainerWidthContextProviderProps ): JSX.Element => {
const [ resizeListener, containerClassName ] = useContainerQueries();
const contextValue = {
@ -50,9 +52,6 @@ export const ContainerWidthContextProvider = ( {
isLarge: containerClassName === 'is-large',
};
/**
* @type {ContainerWidthContext}
*/
return (
<ContainerWidthContext.Provider value={ contextValue }>
<div className={ classNames( className, containerClassName ) }>
@ -62,7 +61,3 @@ export const ContainerWidthContextProvider = ( {
</ContainerWidthContext.Provider>
);
};
ContainerWidthContextProvider.propTypes = {
children: PropTypes.node,
};