Skip to content

Commit

Permalink
components: Fix Shortcut polymorphism (#31555)
Browse files Browse the repository at this point in the history
* components: Fix View polymorphism

* components: Fix shortcut polymorphism

* Update snapshot tests

* Fix selector and update `selector` type to enforce class selector pattern

* Fix view paths after it was moved

* Add more documentation around selector
  • Loading branch information
sarayourfriend authored May 21, 2021
1 parent 7d5fa9d commit 2667ceb
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
10 changes: 9 additions & 1 deletion packages/components/src/ui/context/polymorphic-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,15 @@ export type PolymorphicComponent< T extends As, O > = {
): JSX.Element | null;
( props: PolymorphicComponentProps< O, T > ): JSX.Element | null;
displayName?: string;
selector: string;
/**
* A CSS selector used to fake component interpolation in styled components
* for components not generated by `styled`. Anything passed to `contextConnect`
* will get this property.
*
* We restrict it to a class to align with the already existing class names that
* are generated by the context system.
*/
selector: `.${ string }`;
};

export type CreatePolymorphicComponent< T extends As, P > = (
Expand Down
16 changes: 10 additions & 6 deletions packages/components/src/ui/shortcut/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { Ref } from 'react';
import { useContextSystem, contextConnect } from '../context';
// eslint-disable-next-line no-duplicate-imports
import type { PolymorphicComponentProps } from '../context';
import { View } from '../../view';

export interface ShortcutDescription {
display: string;
Expand All @@ -25,10 +26,12 @@ function Shortcut(
props: PolymorphicComponentProps< Props, 'span' >,
forwardedRef: Ref< any >
): JSX.Element | null {
const { shortcut, className, ...otherProps } = useContextSystem(
props,
'Shortcut'
);
const {
as: asProp = 'span',
shortcut,
className,
...otherProps
} = useContextSystem( props, 'Shortcut' );
if ( ! shortcut ) {
return null;
}
Expand All @@ -44,14 +47,15 @@ function Shortcut(
}

return (
<span
<View
as={ asProp }
className={ className }
aria-label={ ariaLabel }
ref={ forwardedRef }
{ ...otherProps }
>
{ displayText }
</span>
</View>
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`Shortcut should render a span with the shortcut text 1`] = `
<span
class="components-shortcut"
class="components-shortcut css-1mm2cvy-View emotion-0"
data-wp-c16t="true"
data-wp-component="Shortcut"
>
Expand Down
4 changes: 4 additions & 0 deletions packages/components/src/view/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ import styled from '@emotion/styled';
* );
* }
* ```
*
* @type {import('../ui/context').PolymorphicComponent<'div', {}>}
*/
// @ts-ignore
const View = styled.div``;
View.selector = '.components-view';
View.displayName = 'View';

export default View;

0 comments on commit 2667ceb

Please sign in to comment.