A library for rendering web pages or their parts from JSON data.
Page-constructor is a library for rendering web pages or their parts based on JSON data (support for YAML format is to be added later).
When creating pages, component-based approach is used: a page is built using a set of ready-made blocks that can be placed in any order. Each block has a certain type and set of input data parameters.
For the format of input data and list of available blocks, see the documentation.
npm install @gravity-ui/page-constructor
First, we need a React project and some kind of server. For example, you can create a React project using Vite and an Express server, or you can create Next.js application - it will have a client and server side at once.
Install the required dependencies:
npm install @gravity-ui/page-constructor @diplodoc/transform @gravity-ui/uikit
Insert the Page Constructor to the page. To work correctly, it must be wrapped in a PageConstructorProvider:
import {Provider} from '@gravity-ui/uikit';
import {PageConstructor, PageConstructorProvider} from '@gravity-ui/page-constructor';
import '@gravity-ui/page-constructor/styles/styles.scss';
const App = () => {
const content = {
blocks: [
{
type: 'header-block',
title: 'Hello world',
background: {color: '#f0f0f0'},
description:
'**Congratulations!** Have you built a [page-constructor](https://github.com/gravity-ui/page-constructor) into your website',
},
],
};
return (
<Provider>
<PageConstructorProvider>
<PageConstructor content={content} />
</PageConstructorProvider>
</Provider>
);
};
export default App;
This was the simplest example of a connection. In order for YFM markup to work, you need to process content on the server and receive it on the client.
If your server is a separate application, then you need to install page-constructor:
npm install @gravity-ui/page-constructor
To process YFM in all base blocks, call the contentTransformer and pass the content and options there:
const express = require('express');
const app = express();
const {contentTransformer} = require('@gravity-ui/page-constructor/server');
const content = {
blocks: [
{
type: 'header-block',
title: 'Hello world',
background: {color: '#f0f0f0'},
description:
'**Congratulations!** Have you built a [page-constructor](https://github.com/gravity-ui/page-constructor) into your website',
},
],
};
app.get('/content', (req, res) => {
res.send({content: contentTransformer({content, options: {lang: 'en'}})});
});
app.listen(3000);
On the client, add an endpoint call to receive content:
import {Provider} from '@gravity-ui/uikit';
import {PageConstructor, PageConstructorProvider} from '@gravity-ui/page-constructor';
import '@gravity-ui/page-constructor/styles/styles.scss';
import {useEffect, useState} from 'react';
const App = () => {
const [content, setContent] = useState();
useEffect(() => {
(async () => {
const response = await fetch('http://localhost:3000/content').then((r) => r.json());
setContent(response.content);
})();
}, []);
return (
<Provider>
<PageConstructorProvider>
<PageConstructor content={content} />
</PageConstructorProvider>
</Provider>
);
};
export default App;
To start a new project, you can use the ready-made template on Next.js which we have prepared.
Page Constructor Builder - command-line utility for building static pages from YAML configurations using the @gravity-ui/page-constructor
interface PageConstructorProps {
content: PageContent; // Blocks data in JSON format.
shouldRenderBlock?: ShouldRenderBlock; // A function that is invoked when rendering each block and lets you set conditions for its display.
custom?: Custom; // Custom blocks (see `Customization`).
renderMenu?: () => React.ReactNode; // A function that renders the page menu with navigation (we plan to add rendering for the default menu version).
navigation?: NavigationData; // Navigation data for using navigation component in JSON format
isBranded?: boolean; // If true, adds a footer that links to https://gravity-ui.com/. Try BrandFooter component for more customization.
}
interface PageConstructorProviderProps {
isMobile?: boolean; //A flag indicating that the code is executed in mobile mode.
locale?: LocaleContextProps; //Info about the language and domain (used when generating and formatting links).
location?: Location; //API of the browser or router history, the page URL.
analytics?: AnalyticsContextProps; // function to handle analytics event
ssrConfig?: SSR; //A flag indicating that the code is run on the server side.
theme?: 'light' | 'dark'; //Theme to render the page with.
mapsContext?: MapsContextType; //Params for map: apikey, ty
Media/VideoBlock: key prop warning and SSR hydration mismatch
https://github.com/gravity-ui/page-constructor/issues/1542
Labels: 없음
Installing Page Constructor with React 19 adds a nested ReactDOM 18
https://github.com/gravity-ui/page-constructor/issues/1504
Labels: 없음
Navigation: rightItems overflow causes leftItems container to collapse
https://github.com/gravity-ui/page-constructor/issues/1466
Labels: 없음
Yfm styles specificy
https://github.com/gravity-ui/page-constructor/issues/1389
Labels: good first issue
Vite playground
https://github.com/gravity-ui/page-constructor/issues/1388
Labels: help wanted
Hello! In order for us (YANDEX LLC) to accept patches and other contributions from you, you will have to adopt our Yandex Contributor License Agreement (the “CLA”). The current version of the CLA can be found here:
By adopting the CLA, you state the following:
If you agree with these principles, please read and adopt our CLA. By providing us your contributions, you hereby declare that you have already read and adopt our CLA, and we may freely merge your contributions with our corresponding open source project and use it in further in accordance with terms and conditions of the CLA.
If you have already adopted terms and conditions of the CLA, you are able to provide your contributions. When you submit your pull request, please add the following information into it:
I hereby agree to the terms of the CLA available at: [link].
Replace the bracketed text as follows:
It is enough to provide us such notification once.
If you have any questions, please mail us at opensource@yandex-team.ru.