Skip to content

Commit

Permalink
Introduce context provider in view component
Browse files Browse the repository at this point in the history
  • Loading branch information
ksuess committed Oct 11, 2024
1 parent 1b8328c commit 2da1163
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 7 deletions.
1 change: 1 addition & 0 deletions docs/source/blocks/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ editcomponent
block-style-wrapper
extensions
ssr
viewcontext
core/index
```
38 changes: 38 additions & 0 deletions docs/source/blocks/viewcontext.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
myst:
html_meta:
"description": "Sharing a context between blocks"
"property=og:description": "Sharing a context between blocks"
"property=og:title": "context of view"
"keywords": "Volto, React, blocks, view, context"
---

(viewcontext-label)=

# context of `View`

```{versionadded} Volto 18.0.0-alpha.48
```

Sharing a context between blocks.

Volto provides a context of the `View` component for the case that blocks need to know about each other.

Example: tooltips generated from a glossary show up only on the first occurrence on a page.

Initialize the context for your add-on:

```js
config.views.viewContext["volto-slate-glossary"] = [];
```

Get the context and use it:

```js
import { getViewContext } from '@plone/volto/components/theme/View/View';


let matchedGlossaryTerms = getViewContext("volto-slate-glossary");
```


1 change: 1 addition & 0 deletions packages/volto/news/6394.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Introduce context provider in view component. @ksuess
34 changes: 27 additions & 7 deletions packages/volto/src/components/theme/View/View.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Redirect } from 'react-router-dom';
import { createPortal } from 'react-dom';
import { injectIntl } from 'react-intl';
import qs from 'query-string';
import { cloneDeep } from 'lodash';

import {
ContentMetadataTags,
Expand All @@ -29,6 +30,10 @@ import {
import config from '@plone/volto/registry';
import SlotRenderer from '../SlotRenderer/SlotRenderer';


Check failure on line 33 in packages/volto/src/components/theme/View/View.jsx

View workflow job for this annotation

GitHub Actions / ESlint

Replace `⏎const·ViewContext·=·React.createContext();⏎` with `const·ViewContext·=·React.createContext();`
const ViewContext = React.createContext();


/**
* View container class.
* @class View
Expand Down Expand Up @@ -247,13 +252,15 @@ class View extends Component {
}
/>
<SlotRenderer name="aboveContent" content={this.props.content} />
<RenderedView
key={this.props.content['@id']}
content={this.props.content}
location={this.props.location}
token={this.props.token}
history={this.props.history}
/>
<ViewContext.Provider value={cloneDeep(config.views.viewContext)}>
<RenderedView
key={this.props.content['@id']}
content={this.props.content}
location={this.props.location}
token={this.props.token}
history={this.props.history}
/>
</ViewContext.Provider>
<SlotRenderer name="belowContent" content={this.props.content} />
{this.props.content.allow_discussion && (
<Comments pathname={this.props.pathname} />
Expand All @@ -268,6 +275,19 @@ class View extends Component {
}
}


Check failure on line 278 in packages/volto/src/components/theme/View/View.jsx

View workflow job for this annotation

GitHub Actions / ESlint

Delete `⏎`
export function getViewContext(name) {
const viewContext = React.useContext(ViewContext);

Check failure on line 280 in packages/volto/src/components/theme/View/View.jsx

View workflow job for this annotation

GitHub Actions / ESlint

React Hook "React.useContext" is called in function "getViewContext" that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter. React Hook names must start with the word "use"

if (!viewContext) {
throw new Error(

Check failure on line 283 in packages/volto/src/components/theme/View/View.jsx

View workflow job for this annotation

GitHub Actions / ESlint

Replace `⏎······'Using·getViewContext·hook·outside·of·<View>',⏎····` with `'Using·getViewContext·hook·outside·of·<View>'`
'Using getViewContext hook outside of <View>',
);
}

return viewContext[name] || {};
}

export default compose(
injectIntl,
connect(
Expand Down
2 changes: 2 additions & 0 deletions packages/volto/src/config/Views.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,5 @@ export const layoutViewsNamesMapping = {
view: 'Default view',
default: 'Default view',
};

export const viewContext = {};
2 changes: 2 additions & 0 deletions packages/volto/src/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
defaultView,
errorViews,
layoutViewsNamesMapping,
viewContext,
} from './Views';
import { nonContentRoutes } from './NonContentRoutes';
import { nonContentRoutesPublic } from './NonContentRoutesPublic';
Expand Down Expand Up @@ -205,6 +206,7 @@ let config = {
defaultView,
errorViews,
layoutViewsNamesMapping,
viewContext,
},
blocks: {
requiredBlocks,
Expand Down

0 comments on commit 2da1163

Please sign in to comment.