-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
trial using context for renderingTarget (#8704)
- Loading branch information
Showing
58 changed files
with
444 additions
and
283 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
16 changes: 16 additions & 0 deletions
16
dotcom-rendering/.storybook/decorators/configContextDecorator.js
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,16 @@ | ||
import { ConfigProvider } from '../../src/components/ConfigContext'; | ||
|
||
const defaultConfig = { renderingTarget: 'Web' }; | ||
|
||
export const ConfigContextDecorator = (Story, { args: { config } }) => { | ||
const context = { ...defaultConfig, ...config }; | ||
|
||
// For easy debugging | ||
console.log('Storybook application config: \n', context); | ||
|
||
return ( | ||
<ConfigProvider value={context}> | ||
<Story /> | ||
</ConfigProvider> | ||
); | ||
}; |
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
24 changes: 24 additions & 0 deletions
24
dotcom-rendering/docs/architecture/028-react-context-api.md
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,24 @@ | ||
# React Context API | ||
|
||
## Context | ||
|
||
[A decision](dotcom-rendering/docs/architecture/016-react-context-api.md) was made in 2019 to avoid use of the React context API, preferring prop-drilling and avoiding global state. | ||
|
||
[This decision was revisited in 2023](https://github.com/guardian/dotcom-rendering/discussions/8696) due to [the introduction of rendering target as a prop](dotcom-rendering/docs/architecture/proposed-adrs/rendering-type.md) which represents a global state, resulting in very heavy prop-drilling throughout the app. This began to complicate pull requests due to so many unrelated changes appearing in the diff since the props needed to be provided at every layer of the app, as well as tightly coupling components unnecessarily. | ||
|
||
An RFC was put together [here](https://github.com/guardian/dotcom-rendering/pull/8704) to trial using the React context API for this specific type of global state, which doesn't change throughout the component lifecycle ie. immutable application configuration. | ||
|
||
## Decision | ||
|
||
The decision to allow use of context more generally rests on the following _"lines in the sand"_: | ||
|
||
- Context should be considered **global, static, and immutable** for rendered components in dotcom-rendering. | ||
- It can be thought of as a type of configuration for our application. | ||
- Context should **not be used for values that change between re-renders**, since this adds unnecessary complexity and there are alternative solutions for these cases. | ||
- There is a eslint rule to attempt to prevent this (`react/jsx-no-constructed-context-values`) | ||
- Context should **only be used for React components** (ie. not for JSON responses or non-JSX helper code) | ||
- This is because the React context API will not work outside of React | ||
|
||
## Status | ||
|
||
Approved |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,10 @@ | ||
# Storybook | ||
|
||
We use Storybook to visualise our components in an isolated environment, where we can tweak the conditions as we want. | ||
We use Chromatic for visual regression testing of Storybook components. | ||
|
||
## Rendering context | ||
|
||
We use context for static, global state in the dotcom-rendering app, so every story is wrapped in a context provider component. In the real world, our top component includes this context provider. | ||
|
||
In Storybook is largely invisible as it's hidden within the [configuration](dotcom-rendering/.storybook). There's a decorator configured to wrap around stories and log the context output to the console, for easier debugging. |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import type { Config } from '../../types/configContext'; | ||
|
||
/** | ||
* getConfig takes the given html element and returns its config attribute | ||
* | ||
* We expect the element to always be a `gu-*` custom element | ||
* | ||
* @param marker : The html element that we want to read the config attribute from; | ||
* @returns | ||
*/ | ||
export const getConfig = (marker: HTMLElement): Config => { | ||
const serialised = marker.getAttribute('config'); | ||
|
||
try { | ||
if (!serialised) { | ||
throw Error('Unable to fetch config attribute from marker element'); | ||
} else { | ||
return JSON.parse(serialised) as Config; | ||
} | ||
} catch (error: unknown) { | ||
console.error( | ||
`🚨 Error parsing config. Is this data serialisable? ${String( | ||
serialised, | ||
)} 🚨`, | ||
); | ||
throw error; | ||
} | ||
}; |
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.