@grucloud/bau-ui 0.56.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.
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.56.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.56.0",
24
- "@grucloud/bau-css": "^0.56.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": "e5671d456337fd1f4a85e5f6cb0eb3c75e82d340"
29
+ "gitHead": "156435fa3e1d65f18dc46090759a3d679de47c8b"
30
30
  }
@@ -111,61 +111,16 @@ export default function (context, options = {}) {
111
111
 
112
112
  const stepsCreatedState = bau.state([]);
113
113
 
114
- const hashchange = () => {
115
- const search = new URLSearchParams(window.location.search);
116
- const stepNameInitial = search.get(stepperName) ?? stepperDefs[0].name;
117
- const nextActiveStep = Math.max(
118
- stepperDefs.findIndex(({ name }) => name == stepNameInitial),
119
- 0
120
- );
121
- // console.log(
122
- // "hashchange",
123
- // stepNameInitial,
124
- // " stepsCreatedState.val",
125
- // stepsCreatedState.val,
126
- // "nextActiveStep",
127
- // nextActiveStep
128
- // );
129
-
130
- if (nextActiveStep < activeStepIndex.val) {
131
- console.log("remove last step");
132
- stepsCreatedState.val.pop();
133
- }
134
-
135
- if (!stepsCreatedState.val.some(({ name }) => stepNameInitial == name)) {
136
- console.log("add new step");
137
- stepsCreatedState.val.push(stepperDefs[nextActiveStep]);
138
- }
139
- activeStepIndex.val = nextActiveStep;
140
- };
141
-
142
- hashchange();
143
-
144
114
  window.history.pushState = new Proxy(window.history.pushState, {
145
115
  apply: (target, thisArg, argArray) => {
146
116
  target.apply(thisArg, argArray);
147
- console.log("stepper pushState");
148
-
149
117
  const url = argArray[2] ?? "";
118
+ console.log("stepper pushState ", url);
150
119
  if (["?", "#"].includes(url[0])) {
151
120
  hashchange();
152
121
  }
153
122
  },
154
123
  });
155
- document.addEventListener("click", (event) => {
156
- const { target } = event;
157
- const href = target.getAttribute("href");
158
- if (target.tagName === "A" && href && ["?", "#"].includes(href[0])) {
159
- console.log("stepper click");
160
- //hashchange();
161
- event.preventDefault();
162
- }
163
- });
164
- //TODO bauMounted
165
- window.addEventListener("popstate", (event) => {
166
- //console.log("popstate");
167
- hashchange();
168
- });
169
124
 
170
125
  const stepperCurrentState = bau.derive(
171
126
  () => stepperState.val[activeStepIndex.val]
@@ -190,8 +145,35 @@ export default function (context, options = {}) {
190
145
  const getIndex = (step) =>
191
146
  stepperDefs.findIndex(({ name }) => name == step.name);
192
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
+ };
166
+
167
+ hashchange();
168
+
193
169
  return div(
194
170
  {
171
+ bauMounted: ({ element }) => {
172
+ window.addEventListener("popstate", hashchange);
173
+ },
174
+ bauUnmounted: () => {
175
+ window.removeEventListener("popstate", hashchange);
176
+ },
195
177
  class: classNames(
196
178
  "stepper",
197
179
  variant,
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,6 +150,12 @@ 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),
@@ -138,20 +168,6 @@ export default function (context, options = {}) {
138
168
  })
139
169
  );
140
170
 
141
- rootEl.addEventListener(
142
- "tab.select",
143
- (event) => {
144
- const { tabName } = event.detail;
145
- const nextTab = tabByName(tabName);
146
- if (!nextTab) {
147
- return;
148
- }
149
- tabCurrentState.val.exit?.call();
150
- tabCurrentState.val = nextTab;
151
- nextTab.enter?.call();
152
- },
153
- false
154
- );
155
171
  rootEl.addEventListener(
156
172
  "tab.add",
157
173
  (event) => {