@raketa-builder/menu-builder 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@raketa-builder/menu-builder",
3
+ "version": "0.1.0",
4
+ "main": "./dist/index.js",
5
+ "types": "./dist/index.d.ts",
6
+ "license": "MIT",
7
+ "scripts": {
8
+ "lint": "TIMING=1 eslint \"**/*.js\"",
9
+ "build": "tsup src/index.js --format cjs --loader \".js=jsx\"",
10
+ "dev": "tsup src/index.js --format cjs --loader \".js=jsx\" --watch"
11
+ },
12
+ "peerDependencies": {
13
+ "styled-components": "^5.1.0",
14
+ "react": "18.2.0",
15
+ "react-dom": "18.2.0"
16
+ },
17
+ "dependencies": {
18
+ "react-sortablejs": "2.0.11",
19
+ "ui": "*"
20
+ },
21
+ "devDependencies": {
22
+ "@types/react": "17.0.37",
23
+ "@types/react-dom": "17.0.11",
24
+ "eslint": "7.32.0",
25
+ "eslint-config-custom": "*",
26
+ "react": "18.2.0",
27
+ "tsconfig": "*",
28
+ "tsup": "6.5.0",
29
+ "typescript": "4.5.2"
30
+ }
31
+ }
@@ -0,0 +1,44 @@
1
+ import React from "react";
2
+ import styled from "styled-components";
3
+ import { Modal, buttonReset } from "@raketa-builder/ui";
4
+ import WidgetContext from "./WidgetContext";
5
+
6
+ const WidgetButton = styled.button`
7
+ ${buttonReset};
8
+ width: 100%;
9
+ margin-bottom: 0.5em;
10
+ padding: 1em;
11
+ border: 1px solid ${(props) => props.theme.colors.gray};
12
+ border-radius: 3px;
13
+ font-weight: 500;
14
+ text-align: left;
15
+
16
+ &:hover {
17
+ background-color: ${(props) => props.theme.colors.primaryLight};
18
+ border-color: ${(props) => props.theme.colors.primary};
19
+ color: ${(props) => props.theme.colors.primary};
20
+ }
21
+ `;
22
+
23
+ const AddWidget = ({ onAdd, onClose }) => {
24
+ return (
25
+ <Modal open title="Select widget" onClose={onClose}>
26
+ <WidgetContext.Consumer>
27
+ {(WIDGETS) => (
28
+ <React.Fragment>
29
+ {Object.keys(WIDGETS).map((widgetName) => (
30
+ <WidgetButton
31
+ key={widgetName}
32
+ onClick={() => onAdd(widgetName, WIDGETS[widgetName])}
33
+ >
34
+ {widgetName}
35
+ </WidgetButton>
36
+ ))}
37
+ </React.Fragment>
38
+ )}
39
+ </WidgetContext.Consumer>
40
+ </Modal>
41
+ );
42
+ };
43
+
44
+ export default AddWidget;
@@ -0,0 +1,31 @@
1
+ import React from "react";
2
+ import { Button } from "@raketa-builder/ui";
3
+
4
+ const AdminWidget = ({ Component, settings, onChange, onCancel }) => {
5
+ const [state, setState] = React.useState(settings);
6
+ const Form = Component.admin;
7
+
8
+ return (
9
+ <div>
10
+ <Form
11
+ settings={state}
12
+ onChange={(key, value) => {
13
+ setState({ ...state, ...{ [key]: value } });
14
+ }}
15
+ />
16
+
17
+ <Button
18
+ type="submit"
19
+ variant="primary"
20
+ onClick={() => onChange({ ...state })}
21
+ >
22
+ Save
23
+ </Button>
24
+ <Button variant="secondary" onClick={onCancel}>
25
+ Cancel
26
+ </Button>
27
+ </div>
28
+ );
29
+ };
30
+
31
+ export default AdminWidget;
@@ -0,0 +1,138 @@
1
+ import React, { useEffect } from "react";
2
+ import styled from "styled-components";
3
+ import { ReactSortable } from "react-sortablejs";
4
+ import Factories from "./Factories";
5
+ import { append, remove, update, rand } from "./utils";
6
+ import { Copy, Delete } from "./icons";
7
+ import {
8
+ Button,
9
+ Toolbar,
10
+ Stack,
11
+ Handle,
12
+ Panel,
13
+ PanelTitle,
14
+ PanelContent,
15
+ } from "@raketa-builder/ui";
16
+ import ContentBuilder from "./ContentBuilder";
17
+
18
+ const Wrapper = styled.div`
19
+ .r-list {
20
+ display: flex;
21
+ }
22
+ `;
23
+
24
+ const ColumnBuilder = ({ value, onChange }) => {
25
+ const [state, setState] = React.useState(value);
26
+
27
+ useEffect(() => {
28
+ setState(value);
29
+ }, [value]);
30
+
31
+ const addColumn = (items, item) => {
32
+ const newState = {
33
+ ...state,
34
+ columns: [...items, item],
35
+ };
36
+
37
+ setState(newState);
38
+ onChange(newState);
39
+ };
40
+
41
+ const updateColumn = (items, item) => {
42
+ const newState = {
43
+ ...state,
44
+ columns: update(items, item),
45
+ };
46
+
47
+ setState(newState);
48
+ onChange(newState);
49
+ };
50
+
51
+ const duplicateColumn = (items, item) => {
52
+ const newState = {
53
+ ...state,
54
+ columns: append(items, {
55
+ ...item,
56
+ id: rand(),
57
+ label: `${item.label} (copy)`,
58
+ }),
59
+ };
60
+
61
+ setState(newState);
62
+ };
63
+
64
+ const removeColumn = (items, item) => {
65
+ if (!confirm("Are you sure?")) return;
66
+
67
+ const newState = {
68
+ ...state,
69
+ columns: remove(items, item),
70
+ };
71
+
72
+ setState(newState);
73
+ onChange(newState);
74
+ };
75
+
76
+ const reorder = (columns) => {
77
+ const newState = { ...state, columns };
78
+
79
+ setState(newState);
80
+ onChange(newState);
81
+ };
82
+
83
+ return (
84
+ <Wrapper>
85
+ <Toolbar>
86
+ <Button
87
+ variant="secondary"
88
+ onClick={() => addColumn(state.columns, Factories.column())}
89
+ >
90
+ Add column
91
+ </Button>
92
+ </Toolbar>
93
+
94
+ <ReactSortable
95
+ list={state.columns}
96
+ setList={(columns) => reorder(columns)}
97
+ className="r-list"
98
+ handle=".r-chandle"
99
+ direction="vertical"
100
+ >
101
+ {state.columns.map((column, idx) => (
102
+ <Panel key={column.id} style={{ flex: 1, marginRight: "0.5em" }}>
103
+ <PanelTitle>
104
+ <Stack v="center">
105
+ <Handle className="r-chandle" />
106
+ Column #{idx + 1}
107
+ </Stack>
108
+
109
+ <Stack v="center">
110
+ <Button
111
+ variant="secondary"
112
+ onClick={() => duplicateColumn(state.columns, column)}
113
+ >
114
+ <Copy />
115
+ </Button>
116
+ <Button
117
+ variant="secondary"
118
+ onClick={() => removeColumn(state.columns, column)}
119
+ >
120
+ <Delete />
121
+ </Button>
122
+ </Stack>
123
+ </PanelTitle>
124
+
125
+ <PanelContent>
126
+ <ContentBuilder
127
+ value={column}
128
+ onChange={(column) => updateColumn(state.columns, column)}
129
+ />
130
+ </PanelContent>
131
+ </Panel>
132
+ ))}
133
+ </ReactSortable>
134
+ </Wrapper>
135
+ );
136
+ };
137
+
138
+ export default ColumnBuilder;
@@ -0,0 +1,151 @@
1
+ import React, { useEffect } from "react";
2
+ import { ReactSortable } from "react-sortablejs";
3
+ import WidgetContext from "./WidgetContext";
4
+ import Factories from "./Factories";
5
+ import AdminWidget from "./AdminWidget";
6
+ import AddWidget from "./AddWidget";
7
+ import { remove, update } from "./utils";
8
+ import { Edit, Delete } from "./icons";
9
+ import {
10
+ Button,
11
+ Stack,
12
+ Handle,
13
+ Panel,
14
+ PanelTitle,
15
+ Modal,
16
+ } from "@raketa-builder/ui";
17
+
18
+ const getWidget = (widgets, name) => widgets[name];
19
+ const getTitle = (widgets, widget) => {
20
+ const { component } = widget;
21
+ const primaryField = getWidget(widgets, component).primaryField;
22
+ return primaryField
23
+ ? `${component}: ${widget.settings[primaryField]}`
24
+ : component;
25
+ };
26
+
27
+ const ContentBuilder = ({ value, onChange }) => {
28
+ const [widget, setWidget] = React.useState({
29
+ open: false,
30
+ current: null,
31
+ });
32
+ const [open, setOpen] = React.useState(false);
33
+ const [state, setState] = React.useState(value);
34
+
35
+ useEffect(() => {
36
+ setState(value);
37
+ }, [value]);
38
+
39
+ const addWidget = (items, item) => {
40
+ const newState = {
41
+ ...state,
42
+ widgets: [...items, item],
43
+ };
44
+
45
+ setOpen(false);
46
+ setState(newState);
47
+ onChange(newState);
48
+ };
49
+
50
+ const removeWidget = (items, item) => {
51
+ if (!confirm("Are you sure?")) return;
52
+
53
+ const newState = {
54
+ ...state,
55
+ widgets: remove(items, item),
56
+ };
57
+
58
+ setState(newState);
59
+ onChange(newState);
60
+ };
61
+
62
+ const reorder = (widgets) => {
63
+ const newState = { ...state, widgets };
64
+
65
+ setState(newState);
66
+ onChange(newState);
67
+ };
68
+
69
+ const closeWidget = () => setWidget({ open: false, current: null });
70
+
71
+ const updateWidget = (widget, settings) => {
72
+ const newState = {
73
+ ...state,
74
+ widgets: update(state.widgets, { ...widget, settings }),
75
+ };
76
+
77
+ closeWidget();
78
+ setState(newState);
79
+ onChange(newState);
80
+ };
81
+
82
+ return (
83
+ <WidgetContext.Consumer>
84
+ {(WIDGETS) => (
85
+ <React.Fragment>
86
+ <ReactSortable
87
+ list={state.widgets}
88
+ setList={(widgets) => reorder(widgets)}
89
+ handle=".r-handle"
90
+ direction="vertical"
91
+ >
92
+ {state.widgets.map((widget) => (
93
+ <Panel key={widget.id}>
94
+ <PanelTitle>
95
+ <Stack v="center">
96
+ <Handle className="r-handle" />
97
+ {getTitle(WIDGETS, widget)}
98
+ </Stack>
99
+
100
+ <Stack v="center">
101
+ <Button
102
+ variant="secondary"
103
+ onClick={() => setWidget({ open: true, current: widget })}
104
+ >
105
+ <Edit />
106
+ </Button>
107
+ <Button
108
+ variant="secondary"
109
+ onClick={() => removeWidget(state.widgets, widget)}
110
+ >
111
+ <Delete />
112
+ </Button>
113
+ </Stack>
114
+ </PanelTitle>
115
+ </Panel>
116
+ ))}
117
+ </ReactSortable>
118
+
119
+ <Button variant="secondary" onClick={() => setOpen(true)}>
120
+ Add widget
121
+ </Button>
122
+
123
+ {widget.open && (
124
+ <Modal title="Settings" open={widget.open} onClose={closeWidget}>
125
+ <AdminWidget
126
+ Component={getWidget(WIDGETS, widget.current.component)}
127
+ settings={widget.current.settings}
128
+ onChange={(settings) => updateWidget(widget.current, settings)}
129
+ onCancel={closeWidget}
130
+ />
131
+ </Modal>
132
+ )}
133
+
134
+ {open && (
135
+ <AddWidget
136
+ onAdd={(component, widget) =>
137
+ addWidget(
138
+ state.widgets,
139
+ Factories.widget(component, widget.defaults)
140
+ )
141
+ }
142
+ onClose={() => setOpen(false)}
143
+ />
144
+ )}
145
+ </React.Fragment>
146
+ )}
147
+ </WidgetContext.Consumer>
148
+ );
149
+ };
150
+
151
+ export default ContentBuilder;
@@ -0,0 +1,16 @@
1
+ import { rand } from "./utils";
2
+
3
+ const Factories = {
4
+ item: () => ({
5
+ id: rand(4),
6
+ label: "New item",
7
+ url: "#",
8
+ target: "",
9
+ rows: [],
10
+ }),
11
+ row: () => ({ id: rand(4), columns: [] }),
12
+ column: () => ({ id: rand(4), widgets: [] }),
13
+ widget: (component, settings) => ({ id: rand(4), component, settings }),
14
+ };
15
+
16
+ export default Factories;
@@ -0,0 +1,82 @@
1
+ import React from "react";
2
+ import { Copy, Delete } from "./icons";
3
+ import {
4
+ Button,
5
+ Toolbar,
6
+ FormGroup,
7
+ Label,
8
+ Input,
9
+ Select,
10
+ Stack,
11
+ } from "@raketa-builder/ui";
12
+
13
+ const ItemForm = ({ item, onChange, onRemove, onDuplicate }) => (
14
+ <div>
15
+ <Stack>
16
+ <FormGroup>
17
+ <Label htmlFor="label">Label</Label>
18
+
19
+ <Input
20
+ name="label"
21
+ type="text"
22
+ wrapperProps={{ style: { marginRight: "0.5em" } }}
23
+ value={item.label}
24
+ onChange={(e) =>
25
+ onChange({
26
+ ...item,
27
+ label: e.target.value,
28
+ })
29
+ }
30
+ />
31
+ </FormGroup>
32
+
33
+ <FormGroup>
34
+ <Label htmlFor="url">URL</Label>
35
+
36
+ <Input
37
+ name="url"
38
+ type="text"
39
+ value={item.url}
40
+ onChange={(e) =>
41
+ onChange({
42
+ ...item,
43
+ url: e.target.value,
44
+ })
45
+ }
46
+ />
47
+ </FormGroup>
48
+
49
+ <FormGroup>
50
+ <Label htmlFor="target">Target</Label>
51
+
52
+ <Select
53
+ name="target"
54
+ value={item.target}
55
+ onChange={(e) =>
56
+ onChange({
57
+ ...item,
58
+ target: e.target.value,
59
+ })
60
+ }
61
+ >
62
+ <option value="">Please select</option>
63
+ <option value="_self">Open in same tab</option>
64
+ <option value="_blank">Open in new tab</option>
65
+ </Select>
66
+ </FormGroup>
67
+ </Stack>
68
+
69
+ <Toolbar>
70
+ <Stack>
71
+ <Button variant="secondary" onClick={onDuplicate}>
72
+ <Copy />
73
+ </Button>
74
+ <Button variant="secondary" onClick={onRemove}>
75
+ <Delete />
76
+ </Button>
77
+ </Stack>
78
+ </Toolbar>
79
+ </div>
80
+ );
81
+
82
+ export default ItemForm;
@@ -0,0 +1,74 @@
1
+ import React from "react";
2
+ import styled from "styled-components";
3
+ import { ReactSortable } from "react-sortablejs";
4
+ import { append } from "./utils";
5
+ import Factories from "./Factories";
6
+ import {
7
+ reset,
8
+ Button,
9
+ Stack,
10
+ Container,
11
+ buttonReset,
12
+ } from "@raketa-builder/ui";
13
+
14
+ const Item = styled.div`
15
+ ${reset};
16
+ display: inline-flex;
17
+ align-items: center;
18
+ padding: 0;
19
+ margin-right: 0.5em;
20
+ border: 1px solid
21
+ ${(props) =>
22
+ props.active ? props.theme.colors.primary : props.theme.colors.gray};
23
+ border-radius: 48px;
24
+ background-color: ${(props) =>
25
+ props.active ? props.theme.colors.primary : props.theme.colors.white};
26
+ font-weight: 700;
27
+ transition: 0.1s ease-in-out all;
28
+
29
+ &:hover {
30
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
31
+ }
32
+ `;
33
+
34
+ const TabButton = styled.button`
35
+ ${buttonReset};
36
+ padding: 0.5em 1em;
37
+ color: ${(props) =>
38
+ props.active ? props.theme.colors.white : props.theme.colors.black};
39
+ font-weight: 700;
40
+ `;
41
+
42
+ const MainNavigation = ({ items, tab, onTab, onChange }) => (
43
+ <Container bottom>
44
+ <Stack>
45
+ <ReactSortable
46
+ list={items}
47
+ setList={onChange}
48
+ direction="horizontal"
49
+ dragoverBubble
50
+ >
51
+ {items.map((item, idx) => (
52
+ <Item key={item.id} active={idx === tab}>
53
+ <TabButton
54
+ type="button"
55
+ active={idx === tab}
56
+ onClick={() => onTab(idx)}
57
+ >
58
+ {item.label}
59
+ </TabButton>
60
+ </Item>
61
+ ))}
62
+ </ReactSortable>
63
+
64
+ <Button
65
+ variant="secondary"
66
+ onClick={() => onChange(append(items, Factories.item()))}
67
+ >
68
+ Add item
69
+ </Button>
70
+ </Stack>
71
+ </Container>
72
+ );
73
+
74
+ export default MainNavigation;
@@ -0,0 +1,108 @@
1
+ import React, { useEffect } from "react";
2
+ import { ReactSortable } from "react-sortablejs";
3
+ import Factories from "./Factories";
4
+ import { remove, update } from "./utils";
5
+ import ColumnBuilder from "./ColumnBuilder";
6
+ import { Delete } from "./icons";
7
+ import {
8
+ Button,
9
+ Toolbar,
10
+ Stack,
11
+ Handle,
12
+ Panel,
13
+ PanelTitle,
14
+ PanelContent,
15
+ } from "@raketa-builder/ui";
16
+
17
+ const RowBuilder = ({ value, onChange }) => {
18
+ const [state, setState] = React.useState(value);
19
+
20
+ useEffect(() => {
21
+ setState(value);
22
+ }, [value]);
23
+
24
+ const addRow = (items, item) => {
25
+ const newState = {
26
+ ...state,
27
+ rows: [...items, item],
28
+ };
29
+
30
+ setState(newState);
31
+ onChange(newState);
32
+ };
33
+
34
+ const updateRow = (items, item) => {
35
+ const newState = {
36
+ ...state,
37
+ rows: update(items, item),
38
+ };
39
+
40
+ setState(newState);
41
+ onChange(newState);
42
+ };
43
+
44
+ const removeRow = (items, item) => {
45
+ if (!confirm("Are you sure?")) return;
46
+
47
+ const newState = {
48
+ ...state,
49
+ rows: remove(items, item),
50
+ };
51
+
52
+ setState(newState);
53
+ onChange(newState);
54
+ };
55
+
56
+ const reorder = (rows) => {
57
+ const newState = { ...state, rows };
58
+
59
+ setState(newState);
60
+ onChange(newState);
61
+ };
62
+
63
+ return (
64
+ <React.Fragment>
65
+ <ReactSortable
66
+ list={state.rows}
67
+ setList={(rows) => reorder(rows)}
68
+ handle=".r-handle"
69
+ direction="vertical"
70
+ >
71
+ {state.rows.map((row, idx) => (
72
+ <Panel key={row.id}>
73
+ <PanelTitle>
74
+ <Stack v="center">
75
+ <Handle className="r-handle" />
76
+ Row #{idx + 1}
77
+ </Stack>
78
+ <Button
79
+ variant="secondary"
80
+ onClick={() => removeRow(state.rows, row)}
81
+ >
82
+ <Delete />
83
+ </Button>
84
+ </PanelTitle>
85
+
86
+ <PanelContent>
87
+ <ColumnBuilder
88
+ value={row}
89
+ onChange={(row) => updateRow(state.rows, row)}
90
+ />
91
+ </PanelContent>
92
+ </Panel>
93
+ ))}
94
+ </ReactSortable>
95
+
96
+ <Toolbar>
97
+ <Button
98
+ variant="secondary"
99
+ onClick={() => addRow(state.rows, Factories.row())}
100
+ >
101
+ Add row
102
+ </Button>
103
+ </Toolbar>
104
+ </React.Fragment>
105
+ );
106
+ };
107
+
108
+ export default RowBuilder;
@@ -0,0 +1,3 @@
1
+ import React from "react";
2
+
3
+ export default React.createContext();