Repository navigation
Components: Refactor withFallbackStyles from class to function component #78837
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
8f844e2
abf270f
c8c6114
14da661
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,89 +6,60 @@ import fastDeepEqual from 'fast-deep-equal/es6/index.js'; | |
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { Component } from '@wordpress/element'; | ||
| import { createHigherOrderComponent } from '@wordpress/compose'; | ||
| import { useState, useRef } from '@wordpress/element'; | ||
| import { | ||
| createHigherOrderComponent, | ||
| useIsomorphicLayoutEffect, | ||
| } from '@wordpress/compose'; | ||
|
|
||
| type Props = { | ||
| node?: HTMLElement; | ||
| [ key: string ]: any; | ||
| }; | ||
|
|
||
| type State = { | ||
| fallbackStyles?: { [ key: string ]: any }; | ||
| grabStylesCompleted: boolean; | ||
| }; | ||
|
|
||
| export default ( | ||
| mapNodeToProps: ( | ||
| node: HTMLElement, | ||
| props: Props | ||
| ) => { [ key: string ]: any } | ||
| ) => | ||
| createHigherOrderComponent( ( WrappedComponent ) => { | ||
| return class WithFallbackStyles extends Component< Props, State > { | ||
| nodeRef?: HTMLElement; | ||
| return function WithFallbackStyles( props: Props ) { | ||
| const [ fallbackStyles, setFallbackStyles ] = useState< | ||
| { [ key: string ]: any } | undefined | ||
| >( undefined ); | ||
|
|
||
| constructor( props: Props ) { | ||
| super( props ); | ||
| this.nodeRef = this.props.node; | ||
| this.state = { | ||
| fallbackStyles: undefined, | ||
| grabStylesCompleted: false, | ||
| }; | ||
| // Derived from fallbackStyles: the grab is complete once every | ||
| // mapped value has resolved to a truthy style. | ||
| const grabStylesCompleted = | ||
| !! fallbackStyles && | ||
| Object.values( fallbackStyles ).every( Boolean ); | ||
|
|
||
| this.bindRef = this.bindRef.bind( this ); | ||
| } | ||
|
|
||
| bindRef( node: HTMLDivElement ) { | ||
| if ( ! node ) { | ||
| return; | ||
| } | ||
| this.nodeRef = node; | ||
| } | ||
| const nodeRef = useRef< HTMLDivElement | null >( null ); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be shortened to |
||
|
|
||
| componentDidMount() { | ||
| this.grabFallbackStyles(); | ||
| } | ||
|
|
||
| componentDidUpdate() { | ||
| this.grabFallbackStyles(); | ||
| } | ||
|
|
||
| grabFallbackStyles() { | ||
| const { grabStylesCompleted, fallbackStyles } = this.state; | ||
| if ( this.nodeRef && ! grabStylesCompleted ) { | ||
| const newFallbackStyles = mapNodeToProps( | ||
| this.nodeRef, | ||
| this.props | ||
| ); | ||
| // Runs before paint, matching the original componentDidMount / | ||
| // componentDidUpdate, so the fallback styles apply without a flash. | ||
| useIsomorphicLayoutEffect( () => { | ||
| const node = props.node ?? nodeRef.current; | ||
| if ( node && ! grabStylesCompleted ) { | ||
| const newFallbackStyles = mapNodeToProps( node, props ); | ||
|
|
||
| if ( | ||
| ! fastDeepEqual( newFallbackStyles, fallbackStyles ) | ||
| ) { | ||
| this.setState( { | ||
| fallbackStyles: newFallbackStyles, | ||
| grabStylesCompleted: | ||
| Object.values( newFallbackStyles ).every( | ||
| Boolean | ||
| ), | ||
| } ); | ||
| setFallbackStyles( newFallbackStyles ); | ||
| } | ||
| } | ||
| } | ||
| } ); | ||
|
|
||
| const wrappedComponent = ( | ||
| <WrappedComponent { ...props } { ...fallbackStyles } /> | ||
| ); | ||
|
|
||
| render() { | ||
| const wrappedComponent = ( | ||
| <WrappedComponent | ||
| { ...this.props } | ||
| { ...this.state.fallbackStyles } | ||
| /> | ||
| ); | ||
| return this.props.node ? ( | ||
| wrappedComponent | ||
| ) : ( | ||
| <div ref={ this.bindRef }> { wrappedComponent } </div> | ||
| ); | ||
| } | ||
| return props.node ? ( | ||
| wrappedComponent | ||
| ) : ( | ||
| <div ref={ nodeRef }> { wrappedComponent } </div> | ||
| ); | ||
| }; | ||
| }, 'withFallbackStyles' ); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import { render, screen } from '@testing-library/react'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import withFallbackStyles from '..'; | ||
|
|
||
| describe( 'withFallbackStyles', () => { | ||
| const Wrapped = ( props: { fallbackColor?: string } ) => ( | ||
| <div>{ props.fallbackColor ?? 'no-fallback' }</div> | ||
| ); | ||
|
|
||
| it( 'derives the fallback styles from the provided node prop', () => { | ||
| const node = document.createElement( 'span' ); | ||
| node.dataset.color = 'rgb(1, 2, 3)'; | ||
| const mapNodeToProps = jest.fn( ( el: HTMLElement ) => ( { | ||
| fallbackColor: el.dataset.color, | ||
| } ) ); | ||
| const Component = withFallbackStyles( mapNodeToProps )( Wrapped ); | ||
|
|
||
| render( <Component node={ node } /> ); | ||
|
|
||
| // The rendered value proves the actual node was read by mapNodeToProps. | ||
| expect( mapNodeToProps ).toHaveBeenCalledWith( | ||
| node, | ||
| expect.objectContaining( { node } ) | ||
| ); | ||
| expect( screen.getByText( 'rgb(1, 2, 3)' ) ).toBeInTheDocument(); | ||
| } ); | ||
|
|
||
| it( 'derives the fallback styles from the internal wrapper node when no node prop is given', () => { | ||
| const mapNodeToProps = jest.fn( ( el: HTMLElement ) => ( { | ||
| fallbackColor: el.tagName.toLowerCase(), | ||
| } ) ); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be good to test that the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated in 9a14a5a. Both tests now read from the real node ( |
||
| const Component = withFallbackStyles( mapNodeToProps )( Wrapped ); | ||
|
|
||
| render( <Component /> ); | ||
|
|
||
| // The HOC wraps in a <div>, so reading the node's tagName yields 'div'. | ||
| expect( mapNodeToProps ).toHaveBeenCalled(); | ||
| expect( mapNodeToProps.mock.calls[ 0 ][ 0 ].tagName ).toBe( 'DIV' ); | ||
| expect( screen.getByText( 'div' ) ).toBeInTheDocument(); | ||
| } ); | ||
| } ); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be a local variable inside the effect callback. The render function itself doesn't need it.