|
1 | | -# React JS - Components |
| 1 | +# React JS Components |
2 | 2 |
|
3 | | ---- |
| 3 | +This brings Image, Grid and Content Transformer component to ease your rendering when using React JS. |
4 | 4 |
|
5 | | -This repository is what we call a "subtree split": a read-only copy of one directory of the main repository. |
| 5 | +## Installation |
6 | 6 |
|
7 | | -If you want to report or contribute, you should do it on the main repository: https://github.com/CrystallizeAPI/libraries |
| 7 | +With NPM: |
8 | 8 |
|
9 | | ---- |
| 9 | +```bash |
| 10 | +npm install @crystallize/reactjs-components |
| 11 | +``` |
10 | 12 |
|
11 | | -Documentation: https://crystallize.com/learn/open-source/sdks-and-libraries/react-js-components |
| 13 | +With Yarn: |
| 14 | + |
| 15 | +```bash |
| 16 | +yarn add @crystallize/reactjs-components |
| 17 | +``` |
| 18 | + |
| 19 | +## Image |
| 20 | + |
| 21 | +This output an `img` tag with different source variations from Crystallize using _srcset_. Use this to easily build responsive images powered by Crystallize. |
| 22 | + |
| 23 | +```javascript |
| 24 | +import { Image } from '@crystallize/reactjs-components/dist/image'; |
| 25 | +const imageFromCrystallize = { |
| 26 | + url: '...', |
| 27 | + variants: [...] |
| 28 | +} |
| 29 | + |
| 30 | +<Image |
| 31 | + {...imageFromCrystallize} |
| 32 | + sizes="(max-width: 400px) 300w, 700px" |
| 33 | +/> |
| 34 | +``` |
| 35 | + |
| 36 | +There is a live demo: https://crystallizeapi.github.io/libraries/reactjs-components/image |
| 37 | + |
| 38 | +## Video |
| 39 | + |
| 40 | +This output videos from Crystallize using the native video element. |
| 41 | + |
| 42 | +```javascript |
| 43 | +import { Video } from '@crystallize/reactjs-components/dist/video'; |
| 44 | +import '@crystallize/reactjs-components/assets/video/styles.css'; |
| 45 | +const videoFromCrystallize = { |
| 46 | + playlists: [...], |
| 47 | + thumbnails: [...] |
| 48 | +} |
| 49 | + |
| 50 | +<Video |
| 51 | + {...videoFromCrystallize} |
| 52 | + thumbnmailProps={{ sizes: "(max-width: 700px) 90vw, 700px" }} |
| 53 | +/> |
| 54 | +``` |
| 55 | + |
| 56 | +There is a live demo: https://crystallizeapi.github.io/libraries/reactjs-components/video |
| 57 | + |
| 58 | +## Grid |
| 59 | + |
| 60 | +That makes it easy to render Crystallize grids with React JS. In order to use the grid renderer you'll need to have fetched your grid model. This can be fetched fairly easily from Crystallize's API via GraphQL. |
| 61 | + |
| 62 | +At the minimum you will need to fetch layout of each column and some properties on the item. Your query might look something like this: |
| 63 | + |
| 64 | +```graphql |
| 65 | +query grid($id: Int!, $language: String!) { |
| 66 | + grid(id: $id, language: $language) { |
| 67 | + rows { |
| 68 | + columns { |
| 69 | + layout { |
| 70 | + rowspan |
| 71 | + colspan |
| 72 | + colIndex |
| 73 | + rowIndex |
| 74 | + } |
| 75 | + item { |
| 76 | + name |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +Then, inside your component, render the Grid, passing through the grid model as a prop. By default, the grid is rendered using CSS grid but it could also be a Table. |
| 85 | + |
| 86 | +```javascript |
| 87 | +<GridRenderer grid={grid} type={GridRenderingType.Div} cellComponent={Cell} /> |
| 88 | +<GridRenderer grid={grid} type={GridRenderingType.Table} cellComponent={Cell} /> |
| 89 | +<GridRenderer grid={grid} type={GridRenderingType.RowCol} cellComponent={Cell} /> |
| 90 | +``` |
| 91 | + |
| 92 | +There is a live demo: https://crystallizeapi.github.io/libraries/reactjs-components/grid |
| 93 | + |
| 94 | +### To go further |
| 95 | + |
| 96 | +If you want full control over each of the cells, you can instead supply a function as the children of the grid component. This will allow you to iterate over each of the cells and mutate them as you please. |
| 97 | + |
| 98 | +```javascript |
| 99 | +const children = ({ cells }) => { |
| 100 | + return cells.map((cell) => ( |
| 101 | + <div |
| 102 | + style={{ |
| 103 | + gridColumn: `span ${cell.layout.colspan}`, |
| 104 | + gridRow: `span ${cell.layout.rowspan}`, |
| 105 | + }} |
| 106 | + > |
| 107 | + {cell.item.name} |
| 108 | + </div> |
| 109 | + )); |
| 110 | +}; |
| 111 | + |
| 112 | +return ( |
| 113 | + <GridRenderer grid={grid} type={GridRenderingType.Div} cellComponent={Cell}> |
| 114 | + {children} |
| 115 | + </GridRenderer> |
| 116 | +); |
| 117 | +``` |
| 118 | + |
| 119 | +## Content Transformer |
| 120 | + |
| 121 | +This helps you to transform Crystallize rich text json to React html components. |
| 122 | + |
| 123 | +```javascript |
| 124 | +const overrides: Overrides = { |
| 125 | + link: (props: NodeProps) => ( |
| 126 | + <a href={props.metadata?.href}> |
| 127 | + <NodeContent {...props} /> |
| 128 | + </a> |
| 129 | + ), |
| 130 | +}; |
| 131 | + |
| 132 | +<ContentTransformer json={richTextJson} overrides={overrides} />; |
| 133 | +``` |
| 134 | +
|
| 135 | +There is a live demo: https://crystallizeapi.github.io/libraries/reactjs-components/content-transformer |
| 136 | +
|
| 137 | +[crystallizeobject]: crystallize_marketing|folder|6269c9819161f671155d939d |
0 commit comments