- 
                Notifications
    
You must be signed in to change notification settings  - Fork 2.8k
 
feat(react-jsx-runtime): implements custom JSX pragma #27472
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 all commits
c7a45b3
              78d042a
              6accff9
              1bfca4a
              62cce49
              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 | 
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "type": "prerelease", | ||
| "comment": "feat: implements custom JSX pragma", | ||
| "packageName": "@fluentui/react-jsx-runtime", | ||
| "email": "bernardo.sunderhus@gmail.com", | ||
| "dependentChangeType": "patch" | ||
| } | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "type": "minor", | ||
| "comment": "feat: ensure compatibility with custom JSX pragma", | ||
| "packageName": "@fluentui/react-utilities", | ||
| "email": "bernardo.sunderhus@gmail.com", | ||
| "dependentChangeType": "patch" | ||
| } | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| /* eslint-disable jsdoc/check-tag-names */ | ||
| /** @jsxRuntime classic */ | ||
| /** @jsxFrag Fragment */ | ||
| /** @jsx createElement */ | ||
| /* eslint-enable jsdoc/check-tag-names */ | ||
| 
     | 
||
| import { render } from '@testing-library/react'; | ||
| import { ComponentProps, ComponentState, Slot, getSlotsNext, resolveShorthand } from '@fluentui/react-utilities'; | ||
| import { createElement } from './createElement'; | ||
| 
     | 
||
| describe('createElement', () => { | ||
| describe('general behavior tests', () => { | ||
| it('handles a string', () => { | ||
| const result = render(<div>Hello world</div>); | ||
| 
     | 
||
| expect(result.container.firstChild).toMatchInlineSnapshot(` | ||
| <div> | ||
| Hello world | ||
| </div> | ||
| `); | ||
| }); | ||
| 
     | 
||
| it('handles an array', () => { | ||
| const result = render( | ||
| <div> | ||
| {Array.from({ length: 3 }, (_, i) => ( | ||
| <div key={i}>{i}</div> | ||
| ))} | ||
| </div>, | ||
| ); | ||
| 
     | 
||
| expect(result.container.firstChild).toMatchInlineSnapshot(` | ||
| <div> | ||
| <div> | ||
| 0 | ||
| </div> | ||
| <div> | ||
| 1 | ||
| </div> | ||
| <div> | ||
| 2 | ||
| </div> | ||
| </div> | ||
| `); | ||
| }); | ||
| 
     | 
||
| it('handles an array of children', () => { | ||
| const result = render( | ||
| <div> | ||
| <div>1</div> | ||
| <div>2</div> | ||
| </div>, | ||
| ); | ||
| 
     | 
||
| expect(result.container.firstChild).toMatchInlineSnapshot(` | ||
| <div> | ||
| <div> | ||
| 1 | ||
| </div> | ||
| <div> | ||
| 2 | ||
| </div> | ||
| </div> | ||
| `); | ||
| }); | ||
| }); | ||
| 
     | 
||
| describe('custom behavior tests', () => { | ||
| it('keeps children from "defaultProps" in a render callback', () => { | ||
| type TestComponentSlots = { slot: Slot<'div'> }; | ||
| type TestComponentState = ComponentState<TestComponentSlots>; | ||
| type TestComponentProps = ComponentProps<Partial<TestComponentSlots>>; | ||
| 
     | 
||
| const TestComponent = (props: TestComponentProps) => { | ||
| const state: TestComponentState = { | ||
| components: { slot: 'div' }, | ||
| 
     | 
||
| slot: resolveShorthand(props.slot, { | ||
| defaultProps: { children: 'Default Children', id: 'slot' }, | ||
| }), | ||
| }; | ||
| const { slots, slotProps } = getSlotsNext<TestComponentSlots>(state); | ||
| 
     | 
||
| return <slots.slot {...slotProps.slot} />; | ||
| }; | ||
| 
     | 
||
| const children = jest.fn().mockImplementation((Component, props) => ( | ||
| <div id="render-fn"> | ||
| <Component {...props} /> | ||
| </div> | ||
| )); | ||
| const result = render(<TestComponent slot={{ children }} />); | ||
| 
     | 
||
| expect(children).toHaveBeenCalledTimes(1); | ||
| expect(children).toHaveBeenCalledWith('div', { children: 'Default Children', id: 'slot' }); | ||
| 
     | 
||
| expect(result.container.firstChild).toMatchInlineSnapshot(` | ||
| <div | ||
| id="render-fn" | ||
| > | ||
| <div | ||
| id="slot" | ||
| > | ||
| Default Children | ||
| </div> | ||
| </div> | ||
| `); | ||
| }); | ||
| 
     | 
||
| it('keeps children from a render template in a render callback', () => { | ||
| type TestComponentSlots = { outer: Slot<'div'>; inner: Slot<'div'> }; | ||
| type TestComponentState = ComponentState<TestComponentSlots>; | ||
| type TestComponentProps = ComponentProps<Partial<TestComponentSlots>>; | ||
| 
     | 
||
| const TestComponent = (props: TestComponentProps) => { | ||
| const state: TestComponentState = { | ||
| components: { inner: 'div', outer: 'div' }, | ||
| 
     | 
||
| inner: resolveShorthand(props.inner, { defaultProps: { id: 'inner' } }), | ||
| outer: resolveShorthand(props.outer, { defaultProps: { id: 'outer' } }), | ||
| }; | ||
| const { slots, slotProps } = getSlotsNext<TestComponentSlots>(state); | ||
| 
     | 
||
| return ( | ||
| <slots.outer {...slotProps.outer}> | ||
| <slots.inner {...slotProps.inner} /> | ||
| </slots.outer> | ||
| ); | ||
| }; | ||
| 
     | 
||
| const children = jest.fn().mockImplementation((Component, props) => ( | ||
| <div id="render-fn"> | ||
| <Component {...props} /> | ||
| </div> | ||
| )); | ||
| const result = render(<TestComponent outer={{ children }} inner={{ children: 'Inner children' }} />); | ||
| 
     | 
||
| expect(children).toHaveBeenCalledTimes(1); | ||
| expect(children.mock.calls[0][0]).toBe('div'); | ||
| expect(children.mock.calls[0][1].id).toBe('outer'); | ||
| expect(children.mock.calls[0][1].children).toMatchInlineSnapshot(` | ||
| <React.Fragment> | ||
| <div | ||
| id="inner" | ||
| > | ||
| Inner children | ||
| </div> | ||
| </React.Fragment> | ||
| `); | ||
| 
     | 
||
| expect(result.container.firstChild).toMatchInlineSnapshot(` | ||
| <div | ||
| id="render-fn" | ||
| > | ||
| <div | ||
| id="outer" | ||
| > | ||
| <div | ||
| id="inner" | ||
| > | ||
| Inner children | ||
| </div> | ||
| </div> | ||
| </div> | ||
| `); | ||
| }); | ||
| }); | ||
| }); | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import * as React from 'react'; | ||
| import { SlotRenderFunction, UnknownSlotProps, SLOT_RENDER_FUNCTION_SYMBOL } from '@fluentui/react-utilities'; | ||
| 
     | 
||
| type WithMetadata<Props extends {}> = Props & { | ||
| [SLOT_RENDER_FUNCTION_SYMBOL]: SlotRenderFunction<Props>; | ||
| }; | ||
| 
     | 
||
| export function createElement<P extends {}>( | ||
| type: React.ElementType<P>, | ||
| props?: P | null, | ||
| ...children: React.ReactNode[] | ||
| ): React.ReactElement<P> | null { | ||
| if (!isSlotComponent(props)) { | ||
| return React.createElement(type, props, ...children); | ||
| } | ||
| 
     | 
||
| const result = normalizeRenderFunction(props, children); | ||
| return React.createElement( | ||
| React.Fragment, | ||
| {}, | ||
| result.renderFunction(type, { ...result.props, children: result.children }), | ||
| ) as React.ReactElement<P>; | ||
| } | ||
| 
     | 
||
| function normalizeRenderFunction<Props extends UnknownSlotProps>( | ||
| propsWithMetadata: WithMetadata<Props>, | ||
| overrideChildren?: React.ReactNode[], | ||
| ): { | ||
| props: Props; | ||
| children: React.ReactNode; | ||
| renderFunction: SlotRenderFunction<Props>; | ||
| } { | ||
| const { [SLOT_RENDER_FUNCTION_SYMBOL]: renderFunction, children: externalChildren, ...props } = propsWithMetadata; | ||
| 
     | 
||
| const children: React.ReactNode = | ||
| Array.isArray(overrideChildren) && overrideChildren.length > 0 | ||
| ? React.createElement(React.Fragment, {}, ...overrideChildren) | ||
| : externalChildren; | ||
| 
     | 
||
| return { | ||
| children, | ||
| renderFunction, | ||
| props: props as UnknownSlotProps as Props, | ||
| }; | ||
| } | ||
| 
         
      Comment on lines
    
      +8
     to 
      +45
    
   
  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 seems like these functions could be simplified quite a bit, to reduce the number of Fragments and object spreads, and to be easier to understand: export function createElement<P extends {}>(
  type: React.ElementType<P>,
  props?: P | null,
  ...children: React.ReactNode[]
): React.ReactElement<P> | null {
  if (!isSlotComponent(props)) {
    return React.createElement(type, props, children);
  }
  const { [SLOT_RENDER_FUNCTION_SYMBOL]: renderFunction, ...renderProps } = props;
  if (Array.isArray(children) && children.length > 0) {
    renderProps.children = children;
  }
  return React.createElement(React.Fragment, {}, renderFunction(type, renderProps));
}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. 
 in this simplification,  Our tests are even catching this problem: Nonetheless I can see clear simplifications with this method. I'll see to follow up on this with another PR  | 
||
| 
     | 
||
| export function isSlotComponent<Props extends {}>(props?: Props | null): props is WithMetadata<Props> { | ||
| return Boolean(props?.hasOwnProperty(SLOT_RENDER_FUNCTION_SYMBOL)); | ||
| } | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| // TODO: replace with real exports | ||
| export {}; | ||
| export { createElement } from './createElement'; | ||
| export { Fragment } from 'react'; | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| /** | ||
| * @internal | ||
| * Internal reference for the render function | ||
| */ | ||
| export const SLOT_RENDER_FUNCTION_SYMBOL = Symbol('fui.slotRenderFunction'); | 

Uh oh!
There was an error while loading. Please reload this page.