-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for nonce in Fela renderer (#25992)
- Loading branch information
1 parent
9231548
commit 3ec3c77
Showing
9 changed files
with
176 additions
and
108 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
64 changes: 64 additions & 0 deletions
64
packages/fluentui/react-northstar-fela-renderer/src/RendererProvider.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,64 @@ | ||
import { render, rehydrate } from 'fela-dom'; | ||
import * as React from 'react'; | ||
|
||
import { FelaRenderer } from './types'; | ||
|
||
// Copied from https://github.com/robinweser/fela/blob/master/packages/fela-bindings/src/RendererProviderFactory.js | ||
|
||
function hasDOM(renderer: FelaRenderer, targetDocument: Document | undefined) { | ||
// ensure we're on a browser by using document since window is defined in e.g. React Native | ||
// see https://github.com/robinweser/fela/issues/736 | ||
if (typeof document === 'undefined') { | ||
return false; | ||
} | ||
|
||
const doc = targetDocument || document; | ||
|
||
return renderer && doc && doc.createElement; | ||
} | ||
|
||
function hasServerRenderedStyle(targetDocument = document) { | ||
return targetDocument.querySelectorAll('[data-fela-type]').length > 0; | ||
} | ||
|
||
type RendererProviderProps = { | ||
renderer: FelaRenderer; | ||
rehydrate?: boolean; | ||
targetDocument?: Document; | ||
}; | ||
|
||
// Typings provided by "fela" package are outdated, this overrides provides proper definition | ||
declare module 'fela-dom' { | ||
function render(renderer: FelaRenderer, targetDocument?: Document): void; | ||
function rehydrate(renderer: FelaRenderer, targetDocument?: Document): void; | ||
} | ||
|
||
export class RendererProvider extends React.Component<RendererProviderProps, Record<string, unknown>> { | ||
constructor(props: RendererProviderProps) { | ||
super(props); | ||
this._renderStyle(); | ||
} | ||
|
||
componentDidUpdate(prevProps: RendererProviderProps) { | ||
if (prevProps.renderer !== this.props.renderer) { | ||
// add warning that renderer is changed | ||
this._renderStyle(); | ||
} | ||
} | ||
|
||
_renderStyle() { | ||
const { renderer, rehydrate: shouldRehydrate, targetDocument } = this.props; | ||
|
||
if (hasDOM(renderer, targetDocument)) { | ||
if (shouldRehydrate && hasServerRenderedStyle(targetDocument)) { | ||
rehydrate(renderer, targetDocument); | ||
} else { | ||
render(renderer, targetDocument); | ||
} | ||
} | ||
} | ||
|
||
render() { | ||
return <>{this.props.children}</>; | ||
} | ||
} |
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
Oops, something went wrong.