@grucloud/bau-ui 0.55.0 → 0.57.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.
@@ -264,13 +264,6 @@ export default function (context, componentOptions = {}) {
264
264
  `,
265
265
  });
266
266
 
267
- bau.derive(() => {
268
- if (selectedState.val) {
269
- inputShadowEl.value = getOptionValue(selectedState.val);
270
- onSelect(selectedState.val);
271
- }
272
- });
273
-
274
267
  return div(
275
268
  {
276
269
  ...props,
@@ -281,6 +274,15 @@ export default function (context, componentOptions = {}) {
281
274
  props?.class
282
275
  ),
283
276
  },
277
+ bau.bind({
278
+ deps: [selectedState],
279
+ render: () => (selected) => {
280
+ if (selected) {
281
+ inputShadowEl.value = getOptionValue(selected);
282
+ onSelect(selected);
283
+ }
284
+ },
285
+ }),
284
286
  buttonEl,
285
287
  inputShadowEl,
286
288
  popoverEl
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@grucloud/bau-ui",
3
3
  "description": "Components library built with Bau",
4
- "version": "0.55.0",
4
+ "version": "0.57.0",
5
5
  "type": "module",
6
6
  "license": "BSD",
7
7
  "keywords": [
@@ -20,11 +20,11 @@
20
20
  "directory": "bau-ui"
21
21
  },
22
22
  "dependencies": {
23
- "@grucloud/bau": "^0.55.0",
24
- "@grucloud/bau-css": "^0.55.0"
23
+ "@grucloud/bau": "^0.57.0",
24
+ "@grucloud/bau-css": "^0.57.0"
25
25
  },
26
26
  "devDependencies": {
27
27
  "typedoc": "0.24.8"
28
28
  },
29
- "gitHead": "ee5d05d4fdeaf72e84a473e0549af00b867910c7"
29
+ "gitHead": "156435fa3e1d65f18dc46090759a3d679de47c8b"
30
30
  }
@@ -18,6 +18,7 @@ declare module "@grucloud/bau-ui/stepper" {
18
18
  } & StepperPagePropBase;
19
19
 
20
20
  export type StepperProps = {
21
+ stepperName: string;
21
22
  stepperDefs: StepperPage[];
22
23
  };
23
24
 
@@ -25,5 +26,10 @@ declare module "@grucloud/bau-ui/stepper" {
25
26
 
26
27
  type Option = {} & ComponentOption;
27
28
 
29
+ export function NextUrl(
30
+ context: any,
31
+ stepperName: string
32
+ ): (nextStep: string, state?: any) => string;
33
+
28
34
  export default function (context: any, option?: Option): Component;
29
35
  }
package/stepper/index.js CHANGED
@@ -1 +1 @@
1
- export { default } from "./stepper";
1
+ export { default, NextUrl } from "./stepper";
@@ -3,36 +3,41 @@ import classNames from "@grucloud/bau-css/classNames.js";
3
3
 
4
4
  import { Colors } from "../constants.js";
5
5
 
6
- const colorsToCss = () =>
7
- Colors.map(
8
- (color) =>
9
- `
10
- `
11
- ).join("\n");
6
+ const colorsToCss = () => Colors.map((color) => ``).join("\n");
7
+
8
+ export const NextUrl = (context, stepperName) => (nextStep, state) => {
9
+ const search = new URLSearchParams(context.window.location.search);
10
+ search.delete(stepperName);
11
+ search.append(stepperName, nextStep);
12
+ state &&
13
+ Object.entries(state).map(
14
+ ([k, v]) => (search.delete(k), search.append(k, v))
15
+ );
16
+ return `?${search.toString()}`;
17
+ };
12
18
 
13
19
  export default function (context, options = {}) {
14
20
  const { bau, css, window } = context;
15
- const { div, ul, li, span, footer } = bau.tags;
21
+ const { div, ul, li, span, section } = bau.tags;
16
22
 
17
23
  const className = css`
18
24
  display: flex;
19
25
  flex-direction: column;
20
- padding: 1rem;
21
26
  & > ul {
22
27
  display: flex;
23
28
  align-items: flex-end;
24
- justify-content: space-around;
29
+ justify-content: flex-start;
25
30
  align-items: flex-start;
26
31
  padding: 0;
32
+ gap: 1rem;
27
33
  list-style: none;
28
34
  & > li {
29
35
  display: flex;
30
36
  flex-direction: column;
31
37
  align-items: center;
32
38
  box-sizing: border-box;
33
- flex-grow: 1;
39
+ flex-grow: 0;
34
40
  padding: 0.5rem;
35
- padding-bottom: 0rem;
36
41
  color: inherit;
37
42
  font-weight: var(--font-weight-semibold);
38
43
  transition: all var(--transition-slow) ease-in-out;
@@ -76,19 +81,15 @@ export default function (context, options = {}) {
76
81
  }
77
82
  }
78
83
  ${colorsToCss()}
79
- &.stepper > .content {
84
+ &.stepper > section > .content {
80
85
  visibility: hidden;
81
86
  display: none;
82
87
  }
83
88
 
84
- &.stepper > .visible {
89
+ &.stepper > section > .visible {
85
90
  visibility: visible;
86
91
  display: block;
87
92
  }
88
- &.stepper footer {
89
- display: flex;
90
- gap: 1rem;
91
- }
92
93
  `;
93
94
 
94
95
  return function Stepper(...args) {
@@ -98,54 +99,28 @@ export default function (context, options = {}) {
98
99
  variant = "plain",
99
100
  size,
100
101
  stepperDefs = [],
102
+ stepperName,
101
103
  activeStepIndex = bau.state(0),
102
104
  ...props
103
105
  },
104
106
  ...children
105
107
  ] = toPropsAndChildren(args);
106
-
107
108
  const stepperState = bau.state(
108
109
  stepperDefs.map((stepper, index) => ({ ...stepper, index }))
109
110
  );
110
111
 
111
- const hashchange = () => {
112
- const stepNameInitial = window.location.hash.slice(1).split("?")[0];
113
- console.log("hashchange", stepNameInitial);
114
- const initialActiveStep = stepperDefs.findIndex(
115
- ({ name }) => name == stepNameInitial
116
- );
117
-
118
- if (initialActiveStep >= 0) {
119
- activeStepIndex.val = initialActiveStep;
120
- } else {
121
- activeStepIndex.val = 0;
122
- }
123
- };
124
-
125
- hashchange();
112
+ const stepsCreatedState = bau.state([]);
126
113
 
127
114
  window.history.pushState = new Proxy(window.history.pushState, {
128
115
  apply: (target, thisArg, argArray) => {
129
116
  target.apply(thisArg, argArray);
130
117
  const url = argArray[2] ?? "";
118
+ console.log("stepper pushState ", url);
131
119
  if (["?", "#"].includes(url[0])) {
132
120
  hashchange();
133
121
  }
134
122
  },
135
123
  });
136
- document.addEventListener("click", (event) => {
137
- const { target } = event;
138
- const href = target.getAttribute("href");
139
- if (target.tagName === "A" && href && ["?", "#"].includes(href[0])) {
140
- //console.log("a click");
141
- hashchange();
142
- }
143
- });
144
- //TODO bauMounted
145
- window.addEventListener("popstate", (event) => {
146
- //console.log("popstate");
147
- hashchange();
148
- });
149
124
 
150
125
  const stepperCurrentState = bau.derive(
151
126
  () => stepperState.val[activeStepIndex.val]
@@ -167,9 +142,38 @@ export default function (context, options = {}) {
167
142
  span({ class: "step-label" }, () => Header(stepper))
168
143
  );
169
144
  };
145
+ const getIndex = (step) =>
146
+ stepperDefs.findIndex(({ name }) => name == step.name);
147
+
148
+ const hashchange = () => {
149
+ const search = new URLSearchParams(window.location.search);
150
+ const stepNameInitial = search.get(stepperName) ?? stepperDefs[0].name;
151
+ const nextActiveStep = Math.max(
152
+ stepperDefs.findIndex(({ name }) => name == stepNameInitial),
153
+ 0
154
+ );
155
+ if (nextActiveStep < activeStepIndex.val) {
156
+ console.log("remove last step");
157
+ stepsCreatedState.val.pop();
158
+ }
159
+
160
+ if (!stepsCreatedState.val.some(({ name }) => stepNameInitial == name)) {
161
+ console.log("add new step");
162
+ stepsCreatedState.val.push(stepperDefs[nextActiveStep]);
163
+ }
164
+ activeStepIndex.val = nextActiveStep;
165
+ };
170
166
 
171
- const rootEl = div(
167
+ hashchange();
168
+
169
+ return div(
172
170
  {
171
+ bauMounted: ({ element }) => {
172
+ window.addEventListener("popstate", hashchange);
173
+ },
174
+ bauUnmounted: () => {
175
+ window.removeEventListener("popstate", hashchange);
176
+ },
173
177
  class: classNames(
174
178
  "stepper",
175
179
  variant,
@@ -182,8 +186,7 @@ export default function (context, options = {}) {
182
186
  },
183
187
  // Header
184
188
  bau.loop(stepperState, ul(), StepperHeader),
185
- // Content
186
- stepperDefs.map((stepperDef) =>
189
+ bau.loop(stepsCreatedState, section(), (stepperDef) =>
187
190
  div(
188
191
  {
189
192
  class: () =>
@@ -192,11 +195,12 @@ export default function (context, options = {}) {
192
195
  stepperDef.name == stepperCurrentState.val.name && "visible"
193
196
  ),
194
197
  },
195
- () => (stepperDef.Content ? stepperDef.Content(props) : ""),
196
- footer(stepperDef.Footer ? stepperDef.Footer(props) : "")
198
+ stepperDef.Content({
199
+ nextStep: stepperDefs[getIndex(stepperDef) + 1],
200
+ previousStep: stepperDefs[getIndex(stepperDef) - 1],
201
+ })
197
202
  )
198
203
  )
199
204
  );
200
- return rootEl;
201
205
  };
202
206
  }
package/tabs/index.d.ts CHANGED
@@ -23,6 +23,7 @@ declare module "@grucloud/bau-ui/tabs" {
23
23
 
24
24
  type Option = {
25
25
  tabDefs: Tab[];
26
+ tabsName?: string;
26
27
  } & ComponentOption;
27
28
 
28
29
  export default function (context: any, option: Option): Component;
package/tabs/tabs.js CHANGED
@@ -15,7 +15,7 @@ const colorsToCss = () =>
15
15
  export default function (context, options = {}) {
16
16
  const { bau, css, window } = context;
17
17
  const { tabDefs } = options;
18
- const { div, ul, li } = bau.tags;
18
+ const { div, ul, li, a } = bau.tags;
19
19
 
20
20
  const className = css`
21
21
  display: flex;
@@ -26,6 +26,7 @@ export default function (context, options = {}) {
26
26
  justify-content: flex-start;
27
27
  align-items: flex-start;
28
28
  padding: 0;
29
+ margin: 0;
29
30
  list-style: none;
30
31
  & li {
31
32
  & > a {
@@ -83,16 +84,46 @@ export default function (context, options = {}) {
83
84
  size = options.size ?? "md",
84
85
  variant = options.variant ?? "outline",
85
86
  color = options.color ?? "neutral",
87
+ tabsName = "tabs",
86
88
  ...props
87
89
  },
88
90
  ...children
89
91
  ] = toPropsAndChildren(args);
90
92
 
91
93
  const tabsState = bau.state(tabDefs);
92
- const tabNameInitial = window.location.hash.slice(1);
93
-
94
94
  const tabByName = (name) => tabsState.val.find((tab) => tab.name == name);
95
- const tabCurrentState = bau.state(tabByName(tabNameInitial) ?? tabDefs[0]);
95
+ const tabCurrentState = bau.state(tabDefs[0]);
96
+
97
+ const hashchange = () => {
98
+ //console.log("tabs hashchange");
99
+ const search = new URLSearchParams(window.location.search);
100
+ const tabName = search.get(tabsName) ?? tabDefs[0].name;
101
+
102
+ const nextTab = tabByName(tabName);
103
+ tabCurrentState.val.exit?.call();
104
+ tabCurrentState.val = nextTab;
105
+ nextTab.enter?.call();
106
+ };
107
+
108
+ hashchange();
109
+
110
+ window.history.pushState = new Proxy(window.history.pushState, {
111
+ apply: (target, thisArg, argArray) => {
112
+ target.apply(thisArg, argArray);
113
+ const url = argArray[2] ?? "";
114
+ //console.log("tabs pushState ", url);
115
+ if (["?", "#"].includes(url[0])) {
116
+ hashchange();
117
+ }
118
+ },
119
+ });
120
+
121
+ const buildHref = (nextTab) => {
122
+ const search = new URLSearchParams(window.location.search);
123
+ search.delete(tabsName);
124
+ search.append(tabsName, nextTab);
125
+ return `?${search.toString()}`;
126
+ };
96
127
 
97
128
  const TabHeader = (tab) => {
98
129
  const { Header, disabled, name } = tab;
@@ -103,15 +134,8 @@ export default function (context, options = {}) {
103
134
  tabCurrentState.val.name == name && "active",
104
135
  disabled && "disabled"
105
136
  ),
106
- onclick: (event) =>
107
- event.srcElement.dispatchEvent(
108
- new CustomEvent("tab.select", {
109
- detail: { tabName: name },
110
- bubbles: true,
111
- })
112
- ),
113
137
  },
114
- Header(tab)
138
+ a({ href: buildHref(name) }, Header(tab))
115
139
  );
116
140
  };
117
141
 
@@ -126,28 +150,24 @@ export default function (context, options = {}) {
126
150
  options?.class,
127
151
  props.class
128
152
  ),
153
+ bauMounted: ({ element }) => {
154
+ window.addEventListener("popstate", hashchange);
155
+ },
156
+ bauUnmounted: () => {
157
+ window.removeEventListener("popstate", hashchange);
158
+ },
129
159
  },
130
160
  // Header
131
161
  bau.loop(tabsState, ul(), TabHeader),
132
- // Content
133
- () =>
134
- tabCurrentState.val.Content ? tabCurrentState.val.Content(props) : ""
162
+ bau.bind({
163
+ deps: [tabCurrentState],
164
+ render:
165
+ () =>
166
+ ({ Content }) =>
167
+ Content ? Content(props) : "",
168
+ })
135
169
  );
136
170
 
137
- rootEl.addEventListener(
138
- "tab.select",
139
- (event) => {
140
- const { tabName } = event.detail;
141
- const nextTab = tabByName(tabName);
142
- if (!nextTab) {
143
- return;
144
- }
145
- tabCurrentState.val.exit?.call();
146
- tabCurrentState.val = nextTab;
147
- nextTab.enter?.call();
148
- },
149
- false
150
- );
151
171
  rootEl.addEventListener(
152
172
  "tab.add",
153
173
  (event) => {