React & Material UI NPM package boilerplate
A shareable Material UI component library with a playground folder to live-test components as you are developing them.
Technologies used
Procedure to use this boilerplate
- Take a clone of this boilerplate
- Run
npm run i-all to install the dependencies
- Modify the name, description & dependencies in the
package.json and also replace the functional-components-boilerplate in the dependencies of the playground/package.json with your package name
- You can remove the components you don't need from the
src/components
- Create the component following the below structure (Functional Component Structure)
- Export the created component in the
src/index.js file
- Import the newly created component in the
playground/src/App.js file
- Now run
npm run dev to test the component
- If everything is okay, follow the below instructions to publish the package (To publish the package to NPM)
Overall Steps to bring the functional components of the package to the UI Builder
- Customize the default theme of the package (If needed)
- Create the functional component
- For the styling, only use material theme by using the useTheme() hook for both material ui components & also inline styles. Hence it can be customized based on the project
- Export the component in the index.js file
export { default as CustLayout } from './components/layout'
- Run & test the functional component with the help of the playground project
- After testing, publish the npm package with the proper versioning
- Next, add the component JSON in the DB( ArangoDB) (Details Given Below)
- To show the component in the UI Builder, the package needs to be added to the UI Builder code, so contact the UI Builder team. (This is only required for newly created package, after that it won't be required)
Functional Component Structure
import React from 'react';
import Typography from '@material-ui/core/Typography';
import withStyleFix from '../stylefix';
import withTheme from '../themeProvider';
import { useTheme } from '@material-ui/core/styles';
import Container from '@material-ui/core/Container';
const HelloWorld=(props)=>{
const theme = useTheme();
const {name}=props;
return(
<Container disableGutters maxWidth="lg" style={{display:"grid",placeItems:"center",minHeight:"100vh"}}>
<Typography variant="h2" style={{color:theme?.palette?.primary?.main,textAlign:"center",fontWeight:500}}>Hello {name}</Typography>
</Container>
)
}
HelloWorld.defaultProps={
name:"React & Material UI NPM package boilerplate"
}
export default withStyleFix(withTheme(HelloWorld));
Component Sample JSON to put in ArangoDB to show the component in UI Builder
{
"componentName": "CustLayout",
"componentId": "cmuc-comp-1",
"frameWork": "custom-material-ui-component",
"componentType": "Layout",
"isLayout": true,
"supported": [
"web"
],
"defaultProps": {
"childComponent": true
},
"props": {
"ProjectTitle": {
"componentToRender": "messageCatalog"
},
"VersionNo": {
"componentToRender": "text"
},
"Title": {
"componentToRender": "messageCatalog"
},
"SubTitle": {
"componentToRender": "text"
},
"ImageSrc": {
"componentToRender": "imageUpload"
},
"HeaderBackgroundColor": {
"componentToRender": "text"
},
"HeaderFontColor": {
"componentToRender": "text"
},
"Header": {
"componentToRender": "select",
"options": [
true,
false
]
},
"Transtale": {
"componentToRender": "select",
"options": [
true,
false
]
},
"Menu": {
"componentToRender": "select",
"options": [
true,
false
]
},
"Language": {
"componentToRender": "multiSelect",
"options": []
},
"childComponent": {
"componentToRender": "select",
"options": [
true,
false
]
},
"navBarType": {
"componentToRender": "select",
"options": [
"Normal",
"Nested"
]
},
"pages": {
"componentToRender": "pageSelect",
"value": [
{
"name": {
"componentToRender": "text"
},
"page": {
"componentToRender": "text"
},
"childPageSelect": {
"componentToRender": "childPageSelect"
}
}
]
}
},
"active": true
}
Function Component Sample JSON Explanation
{
"componentName": "CustLayout",
"componentId": "cmuc-comp-1",
"frameWork": "functional-components-boilerplate",
"componentType": "Layout",
"isLayout": false,
"supported": [
"web"
],
"defaultProps": {
"childComponent": true
},
"props": {
"key":{
"componentToRender":"text"
}
"ProjectTitle": {
"componentToRender": "messageCatalog"
},
"VersionNo": {
"componentToRender": "text"
},
"Title": {
"componentToRender": "messageCatalog"
},
"SubTitle": {
"componentToRender": "text"
},
"ImageSrc": {
"componentToRender": "imageUpload"
},
"HeaderBackgroundColor": {
"componentToRender": "text"
},
"HeaderFontColor": {
"componentToRender": "text"
},
"Header": {
"componentToRender": "select",
"options": [
true,
false
]
},
"Transtale": {
"componentToRender": "select",
"options": [
true,
false
]
},
"Menu": {
"componentToRender": "select",
"options": [
true,
false
]
},
"Language": {
"componentToRender": "multiSelect",
"options": []
},
"childComponent": {
"componentToRender": "select",
"options": [
true,
false
]
},
"navBarType": {
"componentToRender": "select",
"options": [
"Normal",
"Nested"
]
},
"pages": {
"componentToRender": "pageSelect",
"value": [
{
"name": {
"componentToRender": "text"
},
"page": {
"componentToRender": "text"
},
"childPageSelect": {
"componentToRender": "childPageSelect"
}
}
]
}
},
"active": true
}
Available Scripts
"build": "rollup -c",
"start": "rollup -c -w",
"start-playground": "cd playground && npm run start",
"i-all": "npm i && cd playground && npm i",
"dev": "npm-run-all --parallel start start-playground"
To package and test the created component in the playground Project
- Run
npm run dev to live-test the components
To package and test the created component in a external react project
To bundle and test in local follow below steps. Make sure you have run the npm run build command or npm start before testing.
The above command just include the functional-components-boilerplate in the node_modules (not add anything in package.json).
import React from 'react';
import { HelloWorld } from 'functional-components-boilerplate';
const App = () => {
return <HelloWorld />
}
export default App;
To publish the package to NPM
To publish the package to NPM follow the below setps
- First, build and commit the library
npm run build
git add .
git commit -m 'your commit message'
- Then update the version of the package
npm version patch - For a bug fix
npm version minor - For a new component addition
npm version major - For a completed feature.
- Then login to the NPM account using the below command (ignore if you already logged in)
npm login It asks the NPM credentials
- Finally run
npm publish --access public to publish
- Then, login into the NPM site and check the version update there.
For further more details refere the below link
How to create,test and publish a NPM package in react.
Requirements Status
Changelog
14-06-22
- Boiler Plate created
- Basic instructions to use this boilerplate (
README.md) added
15-06-22
- Configured the theme setup & the style fix for the material ui components
- Added
.npmignore
- IDM.md file removed
- Updated the
README.md file
17-06-21
- Added the
atp-casbin package for the IDM
- Added the instructions to use the
atp-casbin package in the IDM.md file