-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SlotFill: Migrate to Typescript. (#51350)
* convert typescript slot-fill-context.ts and slot-fill-provider.tsx * fix portalContainer type. * convert hooks to ts. * fix types * fix fillProps * convert slot.tsx * update Fill * fix typename. * fix dropdown v2 portal container type. * fix ref type * refactor * refactor SlotFillProvider * migrate SlotComponent to TS * convert useSlot * add update * fix ProviderProps * refactor type * refactor type * allow symbol to name prop. * refactor SlotComponentProps * refactor stroies and README * fix typo * remove comments. remove children from Slot. * refactor SlotComponentProps * Apply suggestions from code review Co-authored-by: Lena Morita <lena@jaguchi.com> * Apply suggestions from code review Co-authored-by: Lena Morita <lena@jaguchi.com> * refactor: newChildren to inline. * Remove unnecessary type variables. * fix type comments * Remove unnecessary Type Guards. Remove unnecessary Type Guards. And use `React.Key`. * remove type from jsdoc * refactor types * refactor types * Simplify the type of `slot`. * Remove the type specification and leave it to type inference. * fix types * remove story.js * update changelog * remove ts-nocheck * fix story filename. remove override bubblesVirtually attribute * replace useState to useMemo * switch to ts-expect-error in story. * fix mssing changelog https://github.com/WordPress/gutenberg/pull/53272/files/362b6d4405edd476df1f6479bb264dc9f51d6789#r1319926144 * add `children?: never` #51350 (comment) * use Record * use Record / Enable className only when bubblesVirtually: true. * Update packages/components/src/slot-fill/types.ts Co-authored-by: Marco Ciampini <marco.ciampo@gmail.com> * update changelog * use optional chain * fix WordPressComponentProps import --------- Co-authored-by: Lena Morita <lena@jaguchi.com> Co-authored-by: Marco Ciampini <marco.ciampo@gmail.com>
- Loading branch information
Showing
21 changed files
with
682 additions
and
377 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 0 additions & 95 deletions
95
packages/components/src/slot-fill/bubbles-virtually/slot-fill-provider.js
This file was deleted.
Oops, something went wrong.
115 changes: 115 additions & 0 deletions
115
packages/components/src/slot-fill/bubbles-virtually/slot-fill-provider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { ref as valRef } from 'valtio'; | ||
import { proxyMap } from 'valtio/utils'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useMemo } from '@wordpress/element'; | ||
import isShallowEqual from '@wordpress/is-shallow-equal'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import SlotFillContext from './slot-fill-context'; | ||
import type { | ||
SlotFillProviderProps, | ||
SlotFillBubblesVirtuallyContext, | ||
} from '../types'; | ||
|
||
function createSlotRegistry(): SlotFillBubblesVirtuallyContext { | ||
const slots: SlotFillBubblesVirtuallyContext[ 'slots' ] = proxyMap(); | ||
const fills: SlotFillBubblesVirtuallyContext[ 'fills' ] = proxyMap(); | ||
|
||
const registerSlot: SlotFillBubblesVirtuallyContext[ 'registerSlot' ] = ( | ||
name, | ||
ref, | ||
fillProps | ||
) => { | ||
const slot = slots.get( name ); | ||
|
||
slots.set( | ||
name, | ||
valRef( { | ||
...slot, | ||
ref: ref || slot?.ref, | ||
fillProps: fillProps || slot?.fillProps || {}, | ||
} ) | ||
); | ||
}; | ||
|
||
const unregisterSlot: SlotFillBubblesVirtuallyContext[ 'unregisterSlot' ] = | ||
( name, ref ) => { | ||
// Make sure we're not unregistering a slot registered by another element | ||
// See https://github.com/WordPress/gutenberg/pull/19242#issuecomment-590295412 | ||
if ( slots.get( name )?.ref === ref ) { | ||
slots.delete( name ); | ||
} | ||
}; | ||
|
||
const updateSlot: SlotFillBubblesVirtuallyContext[ 'updateSlot' ] = ( | ||
name, | ||
fillProps | ||
) => { | ||
const slot = slots.get( name ); | ||
if ( ! slot ) { | ||
return; | ||
} | ||
|
||
if ( isShallowEqual( slot.fillProps, fillProps ) ) { | ||
return; | ||
} | ||
|
||
slot.fillProps = fillProps; | ||
const slotFills = fills.get( name ); | ||
if ( slotFills ) { | ||
// Force update fills. | ||
slotFills.map( ( fill ) => fill.current.rerender() ); | ||
} | ||
}; | ||
|
||
const registerFill: SlotFillBubblesVirtuallyContext[ 'registerFill' ] = ( | ||
name, | ||
ref | ||
) => { | ||
fills.set( name, valRef( [ ...( fills.get( name ) || [] ), ref ] ) ); | ||
}; | ||
|
||
const unregisterFill: SlotFillBubblesVirtuallyContext[ 'registerFill' ] = ( | ||
name, | ||
ref | ||
) => { | ||
const fillsForName = fills.get( name ); | ||
if ( ! fillsForName ) { | ||
return; | ||
} | ||
|
||
fills.set( | ||
name, | ||
valRef( fillsForName.filter( ( fillRef ) => fillRef !== ref ) ) | ||
); | ||
}; | ||
|
||
return { | ||
slots, | ||
fills, | ||
registerSlot, | ||
updateSlot, | ||
unregisterSlot, | ||
registerFill, | ||
unregisterFill, | ||
}; | ||
} | ||
|
||
export default function SlotFillProvider( { | ||
children, | ||
}: SlotFillProviderProps ) { | ||
const registry = useMemo( createSlotRegistry, [] ); | ||
return ( | ||
<SlotFillContext.Provider value={ registry }> | ||
{ children } | ||
</SlotFillContext.Provider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.