@eldrajs/theme-cli 0.2.6 → 0.2.8
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/CHANGELOG.md +2 -0
- package/package.json +3 -3
- package/template/.eldra/block-types.d.ts +1 -1
- package/template/.eldra/manifest.json +162 -132
- package/template/.eldra/previews.json +35 -35
- package/template/app/storefront/commerce.ts +12 -8
- package/template/app/storefront/currency.ts +94 -27
- package/template/app/storefront/feedback.ts +1 -0
- package/template/blocks/announcement-bar/preview.png +0 -0
- package/template/blocks/article/preview.png +0 -0
- package/template/blocks/article-list/preview.png +0 -0
- package/template/blocks/breadcrumbs/preview.png +0 -0
- package/template/blocks/cart/__tests__/notSold.spec.ts +41 -0
- package/template/blocks/cart/parts/NotSoldNotice.vue +7 -2
- package/template/blocks/cart/preview.png +0 -0
- package/template/blocks/collection-grid/preview.png +0 -0
- package/template/blocks/collection-header/preview.png +0 -0
- package/template/blocks/contact/preview.png +0 -0
- package/template/blocks/cta/preview.png +0 -0
- package/template/blocks/faq/preview.png +0 -0
- package/template/blocks/feature-grid/preview.png +0 -0
- package/template/blocks/footer/preview.png +0 -0
- package/template/blocks/form/preview.png +0 -0
- package/template/blocks/gallery/preview.png +0 -0
- package/template/blocks/hero/preview.png +0 -0
- package/template/blocks/image/preview.png +0 -0
- package/template/blocks/logo-cloud/preview.png +0 -0
- package/template/blocks/navigation/Block.vue +54 -11
- package/template/blocks/navigation/__tests__/Block.spec.ts +91 -8
- package/template/blocks/navigation/block.json +11 -3
- package/template/blocks/navigation/mock.json +134 -129
- package/template/blocks/navigation/preview.png +0 -0
- package/template/blocks/newsletter/preview.png +0 -0
- package/template/blocks/order-status/preview.png +0 -0
- package/template/blocks/pricing-table/preview.png +0 -0
- package/template/blocks/product-carousel/preview.png +0 -0
- package/template/blocks/product-detail/Block.vue +29 -7
- package/template/blocks/product-detail/__tests__/Block.spec.ts +112 -1
- package/template/blocks/product-detail/preview.png +0 -0
- package/template/blocks/quote/preview.png +0 -0
- package/template/blocks/rich-text/preview.png +0 -0
- package/template/blocks/search/preview.png +0 -0
- package/template/blocks/split-content/preview.png +0 -0
- package/template/blocks/stats/preview.png +0 -0
- package/template/blocks/tabs/preview.png +0 -0
- package/template/blocks/team/preview.png +0 -0
- package/template/blocks/testimonials/preview.png +0 -0
- package/template/blocks/timeline/preview.png +0 -0
- package/template/blocks/trust-strip/preview.png +0 -0
- package/template/blocks/video-embed/preview.png +0 -0
- package/template/blocks/wishlist/preview.png +0 -0
- package/template/i18n/en-US.json +1 -0
- package/template/i18n/is-IS.json +1 -0
- package/template/package.json +1 -1
- package/template/public/images/bowl.webp +0 -0
- package/template/test/prerenderFailure.spec.ts +4 -3
- package/template/test/previewParity.browser.spec.ts +1152 -0
- package/template/test/starter.spec.ts +6 -1
- package/template/test/storefront/currencyDefault.spec.ts +475 -0
- package/template/test/support/previewParity.ts +302 -0
- package/template/test/support/previewParityContent.ts +726 -0
|
@@ -4,13 +4,15 @@ import { resolveVisitorCurrency, VISITOR_COUNTRY_PATH } from '@eldrajs/theme-cor
|
|
|
4
4
|
import type { CartStore } from './cart';
|
|
5
5
|
import type { StorefrontFailure } from './feedback';
|
|
6
6
|
|
|
7
|
-
/** The visitor's currency, derived: the cart's, else the preference, else
|
|
7
|
+
/** The visitor's currency, derived: the cart's, else the preference, else the default (docs/starter-kit.md, "Currencies"). */
|
|
8
8
|
export interface CurrencyStore {
|
|
9
|
-
/** The store's own currency, or `undefined` when it publishes none. */
|
|
9
|
+
/** The store's own currency, or `undefined` when it publishes none. Offered or not, every product has a price in it. */
|
|
10
10
|
base: string | undefined;
|
|
11
|
-
/** What
|
|
11
|
+
/** What pages are built in and a visitor starts in: `base` unless the store turned it off. */
|
|
12
|
+
defaultCurrency: string | undefined;
|
|
13
|
+
/** What the visitor may pick from, `defaultCurrency` first. One entry or none: no picker. */
|
|
12
14
|
offered: Ref<readonly string[]>;
|
|
13
|
-
/** The currency the site shows: `
|
|
15
|
+
/** The currency the site shows: `defaultCurrency` until `start` settles, then the cart's, the preference's or the default. */
|
|
14
16
|
active: ComputedRef<string | undefined>;
|
|
15
17
|
/** `start` has settled. */
|
|
16
18
|
started: Ref<boolean>;
|
|
@@ -18,15 +20,20 @@ export interface CurrencyStore {
|
|
|
18
20
|
pending: ComputedRef<boolean>;
|
|
19
21
|
/** A move of the cart is in flight or queued. */
|
|
20
22
|
busy: ComputedRef<boolean>;
|
|
21
|
-
/** The currency catalog reads ask for, or `undefined` to read in `base
|
|
23
|
+
/** The currency catalog reads ask for, or `undefined` to read in `base`, as a read naming none does. */
|
|
22
24
|
requested: ComputedRef<string | undefined>;
|
|
25
|
+
/** A business customer, whose cart the store keeps in `base` even while it is not offered: no picker, no move. */
|
|
26
|
+
ownCurrencyOnly: Ref<boolean>;
|
|
23
27
|
/** Set when the visitor was moved off a currency, for a one-time notice. */
|
|
24
28
|
movedTo: Ref<string | null>;
|
|
25
29
|
/** A cart move that failed for another reason than the currency, for an error toast. */
|
|
26
30
|
failure: Ref<StorefrontFailure | null>;
|
|
27
31
|
/** The visitor's explicit choice. With a cart it moves the cart, last choice wins. */
|
|
28
32
|
choose(code: string): void;
|
|
33
|
+
/** Chooses `defaultCurrency`; named for when the default was always the store's own. */
|
|
29
34
|
chooseBase(): void;
|
|
35
|
+
/** For a theme with business sign-in, on every page load while signed in: keeps them in `base` and moves a cart there; `false` on sign-out. */
|
|
36
|
+
keepOwnCurrency(on?: boolean): void;
|
|
30
37
|
/** Stops following other tabs; for a store made for one component (a demo, a story), not the app's. */
|
|
31
38
|
dispose(): void;
|
|
32
39
|
/** Reads the stored preference, else the visitor's country. Call once, after mount. */
|
|
@@ -34,6 +41,7 @@ export interface CurrencyStore {
|
|
|
34
41
|
}
|
|
35
42
|
|
|
36
43
|
export const CURRENCY_NOT_SOLD = 'CART_CURRENCY_NOT_SOLD';
|
|
44
|
+
export const CUSTOMER_ISK_ONLY = 'CART_CURRENCY_CUSTOMER_ISK_ONLY';
|
|
37
45
|
const CURRENCY_PREFERENCE_KEY = 'eldra.currency';
|
|
38
46
|
|
|
39
47
|
function refusedForCurrency(failure: StorefrontFailure | null): boolean {
|
|
@@ -42,42 +50,61 @@ function refusedForCurrency(failure: StorefrontFailure | null): boolean {
|
|
|
42
50
|
|
|
43
51
|
/** Builds the store over `cart`; the returned cart's `add` retries once when its currency is refused. */
|
|
44
52
|
export function createCurrencyStore(
|
|
45
|
-
commerce: { currency: string; currencies?: readonly string[] } | null,
|
|
53
|
+
commerce: { currency: string; currencies?: readonly string[]; defaultCurrency?: string } | null,
|
|
46
54
|
cart: CartStore,
|
|
47
55
|
preference: EldraCurrencyPreference = createCurrencyPreference()
|
|
48
56
|
): { currency: CurrencyStore; cart: CartStore } {
|
|
49
57
|
const base = commerce?.currency;
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
58
|
+
const defaultCurrency = commerce?.defaultCurrency ?? base;
|
|
59
|
+
const listed: readonly string[] =
|
|
60
|
+
defaultCurrency === undefined ? [] : (commerce?.currencies ?? [defaultCurrency]);
|
|
61
|
+
const offered = ref<readonly string[]>(listed);
|
|
53
62
|
const preferred = ref<string | null>(null);
|
|
54
63
|
const started = ref(false);
|
|
55
64
|
const movedTo = ref<string | null>(null);
|
|
56
65
|
const failure = ref<StorefrontFailure | null>(null);
|
|
66
|
+
const ownCurrencyOnly = ref(false);
|
|
67
|
+
let signInChanges = 0;
|
|
68
|
+
const fallback = (): string | undefined => (ownCurrencyOnly.value ? base : defaultCurrency);
|
|
57
69
|
|
|
58
70
|
const active = computed(() => {
|
|
59
|
-
if (!started.value) return
|
|
71
|
+
if (!started.value) return defaultCurrency;
|
|
60
72
|
const own = cart.currency.value;
|
|
61
73
|
if (own !== undefined) return own;
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
: base;
|
|
74
|
+
if (preferred.value !== null && offered.value.includes(preferred.value)) return preferred.value;
|
|
75
|
+
return fallback();
|
|
65
76
|
});
|
|
66
77
|
const requested = computed(() => (active.value === base ? undefined : active.value));
|
|
67
78
|
const pending = computed(() => !started.value && offered.value.length > 1);
|
|
68
79
|
|
|
69
80
|
function withdraw(code: string): void {
|
|
70
|
-
if (code ===
|
|
81
|
+
if (code === defaultCurrency) return;
|
|
71
82
|
offered.value = offered.value.filter((offer) => offer !== code);
|
|
72
83
|
if (preference.read() === code) preference.forget();
|
|
73
84
|
if (preferred.value === code) preferred.value = null;
|
|
74
85
|
}
|
|
75
86
|
|
|
87
|
+
async function keepInBase(): Promise<void> {
|
|
88
|
+
if (base === undefined) return;
|
|
89
|
+
ownCurrencyOnly.value = true;
|
|
90
|
+
offered.value = [base];
|
|
91
|
+
const own = cart.currency.value;
|
|
92
|
+
if (own !== undefined && own !== base) await move(base, true);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function leaveBase(): void {
|
|
96
|
+
ownCurrencyOnly.value = false;
|
|
97
|
+
offered.value = listed;
|
|
98
|
+
}
|
|
99
|
+
|
|
76
100
|
// Moves go through the cart's own write queue, where a newer choice replaces a waiting one.
|
|
77
101
|
const busy = computed(() => cart.pending.value);
|
|
78
102
|
|
|
79
103
|
async function move(code: string, notice = false): Promise<void> {
|
|
104
|
+
const sentAs = signInChanges;
|
|
80
105
|
const answer = await cart.setCurrency(code);
|
|
106
|
+
// An answer about who the visitor was before they signed in or out says nothing about who they are now.
|
|
107
|
+
const sameVisitor = sentAs === signInChanges;
|
|
81
108
|
// A later choice replaced this one before it was sent.
|
|
82
109
|
if (answer === undefined) return;
|
|
83
110
|
if (answer === null) {
|
|
@@ -87,9 +114,18 @@ export function createCurrencyStore(
|
|
|
87
114
|
preference.remember(code);
|
|
88
115
|
} else if (cart.currency.value === code) {
|
|
89
116
|
preferred.value = code;
|
|
90
|
-
|
|
117
|
+
// A business customer's ISK is the store's rule, not a choice to keep for whoever comes next.
|
|
118
|
+
if (!ownCurrencyOnly.value) preference.remember(code);
|
|
91
119
|
if (notice) movedTo.value = code;
|
|
92
120
|
}
|
|
121
|
+
} else if (answer.errorId === CUSTOMER_ISK_ONLY) {
|
|
122
|
+
// The store's own move off a dropped currency is no choice of the visitor's, so nothing to report.
|
|
123
|
+
if (!notice) failure.value = answer;
|
|
124
|
+
if (sameVisitor) await keepInBase();
|
|
125
|
+
} else if (ownCurrencyOnly.value && code === base && refusedForCurrency(answer)) {
|
|
126
|
+
// The store sees no business customer after all (signed out, or no token on the cart): back to what it offers.
|
|
127
|
+
leaveBase();
|
|
128
|
+
if (!notice) failure.value = answer;
|
|
93
129
|
} else {
|
|
94
130
|
if (refusedForCurrency(answer)) withdraw(code);
|
|
95
131
|
failure.value = answer;
|
|
@@ -102,14 +138,15 @@ export function createCurrencyStore(
|
|
|
102
138
|
|
|
103
139
|
const leavingDropped = (): boolean => {
|
|
104
140
|
const own = cart.currency.value;
|
|
105
|
-
return own !== undefined &&
|
|
141
|
+
return own !== undefined && defaultCurrency !== undefined && !offered.value.includes(own);
|
|
106
142
|
};
|
|
107
143
|
|
|
108
|
-
// A cart in a currency this build no longer offers is moved to the
|
|
144
|
+
// A cart in a currency this build no longer offers is moved to the default; the watch fires on a change only, so a failed move is not retried in a loop.
|
|
109
145
|
watch([started, cart.currency], ([isStarted, own]) => {
|
|
110
|
-
|
|
146
|
+
const to = fallback();
|
|
147
|
+
if (!isStarted || own === undefined || to === undefined) return;
|
|
111
148
|
if (offered.value.includes(own)) return;
|
|
112
|
-
void move(
|
|
149
|
+
void move(to, true);
|
|
113
150
|
});
|
|
114
151
|
|
|
115
152
|
const onStorage = (event: StorageEvent): void => {
|
|
@@ -122,6 +159,7 @@ export function createCurrencyStore(
|
|
|
122
159
|
|
|
123
160
|
const currency: CurrencyStore = {
|
|
124
161
|
base,
|
|
162
|
+
defaultCurrency,
|
|
125
163
|
offered,
|
|
126
164
|
active,
|
|
127
165
|
started,
|
|
@@ -130,6 +168,19 @@ export function createCurrencyStore(
|
|
|
130
168
|
requested,
|
|
131
169
|
movedTo,
|
|
132
170
|
failure,
|
|
171
|
+
ownCurrencyOnly,
|
|
172
|
+
keepOwnCurrency(on = true) {
|
|
173
|
+
signInChanges += 1;
|
|
174
|
+
if (on) {
|
|
175
|
+
void keepInBase();
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
leaveBase();
|
|
179
|
+
const own = cart.currency.value;
|
|
180
|
+
if (own !== undefined && defaultCurrency !== undefined && !offered.value.includes(own)) {
|
|
181
|
+
void move(defaultCurrency, true);
|
|
182
|
+
}
|
|
183
|
+
},
|
|
133
184
|
choose(code) {
|
|
134
185
|
if (!offered.value.includes(code)) return;
|
|
135
186
|
// Only with no cart and nothing in flight is a choice just a preference; else it queues behind that work.
|
|
@@ -144,12 +195,16 @@ export function createCurrencyStore(
|
|
|
144
195
|
if (listens) window.removeEventListener('storage', onStorage);
|
|
145
196
|
},
|
|
146
197
|
chooseBase() {
|
|
147
|
-
if (
|
|
198
|
+
if (defaultCurrency !== undefined) currency.choose(defaultCurrency);
|
|
148
199
|
},
|
|
149
200
|
async start(country) {
|
|
150
201
|
let forgotten = false;
|
|
151
|
-
|
|
152
|
-
|
|
202
|
+
const stored = defaultCurrency === undefined ? null : preference.read();
|
|
203
|
+
// With one currency there is no picker and nothing on screen moves, so a stale choice goes quietly.
|
|
204
|
+
const quietlyStale =
|
|
205
|
+
stored !== null && stored !== defaultCurrency && offered.value.length === 1;
|
|
206
|
+
if (quietlyStale && defaultCurrency !== base) preference.forget();
|
|
207
|
+
if (defaultCurrency !== undefined && offered.value.length > 1) {
|
|
153
208
|
if (stored !== null && offered.value.includes(stored)) {
|
|
154
209
|
if (preferred.value === null) preferred.value = stored;
|
|
155
210
|
} else {
|
|
@@ -159,16 +214,17 @@ export function createCurrencyStore(
|
|
|
159
214
|
// A choice made during the lookup, or a cart, wins over the country.
|
|
160
215
|
if (cart.currency.value === undefined && preferred.value === null) {
|
|
161
216
|
const fromCountry = resolveVisitorCurrency({
|
|
162
|
-
base,
|
|
217
|
+
base: defaultCurrency,
|
|
163
218
|
offered: offered.value,
|
|
164
219
|
country: visitorCountry,
|
|
165
220
|
});
|
|
166
|
-
if (fromCountry !==
|
|
221
|
+
if (fromCountry !== defaultCurrency) preferred.value = fromCountry;
|
|
167
222
|
}
|
|
168
223
|
}
|
|
169
224
|
}
|
|
170
225
|
started.value = true;
|
|
171
|
-
|
|
226
|
+
// A cart in a dropped currency is about to move; the move says where prices are.
|
|
227
|
+
if (forgotten && !leavingDropped()) movedTo.value = active.value ?? null;
|
|
172
228
|
},
|
|
173
229
|
};
|
|
174
230
|
|
|
@@ -176,12 +232,23 @@ export function createCurrencyStore(
|
|
|
176
232
|
...cart,
|
|
177
233
|
async add(input) {
|
|
178
234
|
// The visitor's next cart action tries again to leave a currency the store no longer offers.
|
|
179
|
-
|
|
235
|
+
const to = fallback();
|
|
236
|
+
if (leavingDropped() && to !== undefined) await move(to, true);
|
|
180
237
|
const hadCart = cart.currency.value !== undefined;
|
|
181
238
|
const sentIn = active.value;
|
|
239
|
+
const sentAs = signInChanges;
|
|
182
240
|
const answer = await cart.add(input);
|
|
241
|
+
if (answer?.errorId === CUSTOMER_ISK_ONLY && sentIn !== base && sentAs === signInChanges) {
|
|
242
|
+
await keepInBase();
|
|
243
|
+
return cart.add(input);
|
|
244
|
+
}
|
|
183
245
|
// The store checks the currency only when the add creates the cart.
|
|
184
|
-
if (
|
|
246
|
+
if (
|
|
247
|
+
hadCart ||
|
|
248
|
+
!refusedForCurrency(answer) ||
|
|
249
|
+
sentIn === undefined ||
|
|
250
|
+
sentIn === defaultCurrency
|
|
251
|
+
) {
|
|
185
252
|
return answer;
|
|
186
253
|
}
|
|
187
254
|
withdraw(sentIn);
|
|
@@ -75,6 +75,7 @@ const MESSAGE_BY_ERROR_ID: Record<string, MessageKey> = {
|
|
|
75
75
|
// store has moved on from, and reloading is the one thing that fixes it.
|
|
76
76
|
CART_NOT_FOUND: 'storefront.cartOutOfDate',
|
|
77
77
|
CART_ITEM_NOT_FOUND: 'storefront.cartOutOfDate',
|
|
78
|
+
CART_CURRENCY_CUSTOMER_ISK_ONLY: 'storefront.businessOwnCurrency',
|
|
78
79
|
// Refused in the browser: the cart could not be read, so nothing was sent.
|
|
79
80
|
CART_NOT_LOADED: 'cart.loadFailed',
|
|
80
81
|
};
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -107,3 +107,44 @@ describe('a cart with a line not sold in its currency', () => {
|
|
|
107
107
|
expect(checkout?.attributes('href')).toBe('/checkout/demo-cart');
|
|
108
108
|
});
|
|
109
109
|
});
|
|
110
|
+
|
|
111
|
+
describe('the way out with the store currency turned off (WS-1399)', () => {
|
|
112
|
+
async function mountIn(cartCurrency: string) {
|
|
113
|
+
const storefront = createDemoStorefront({
|
|
114
|
+
cartLines: [NOT_SOLD, SOLD],
|
|
115
|
+
commerce: {
|
|
116
|
+
currency: 'USD',
|
|
117
|
+
taxInclusivePricing: false,
|
|
118
|
+
defaultTaxRate: 0,
|
|
119
|
+
currencies: ['EUR', 'GBP'],
|
|
120
|
+
defaultCurrency: 'EUR',
|
|
121
|
+
},
|
|
122
|
+
});
|
|
123
|
+
await flushPromises();
|
|
124
|
+
storefront.cart.currency.value = cartCurrency;
|
|
125
|
+
const options = mountOptions({ entry: { id: 'cart', data: { ...mock, variant: 'page' } } });
|
|
126
|
+
const wrapper = mount(Block, {
|
|
127
|
+
...options,
|
|
128
|
+
attachTo: document.body,
|
|
129
|
+
global: {
|
|
130
|
+
...options.global,
|
|
131
|
+
provide: { ...options.global.provide, [STOREFRONT_KEY]: storefront },
|
|
132
|
+
stubs: { ...options.global.stubs, transition: false, 'transition-group': false },
|
|
133
|
+
},
|
|
134
|
+
});
|
|
135
|
+
await flushPromises();
|
|
136
|
+
return wrapper
|
|
137
|
+
.get('[data-eldra-not-sold-notice]')
|
|
138
|
+
.findAll('button')
|
|
139
|
+
.map((node) => node.text());
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
it('offers the default currency from another one', async () => {
|
|
143
|
+
expect(await mountIn('GBP')).toContain('Shop in EUR');
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('offers no way out from the default itself', async () => {
|
|
147
|
+
const buttons = await mountIn('EUR');
|
|
148
|
+
expect(buttons).toEqual(['Remove and check out']);
|
|
149
|
+
});
|
|
150
|
+
});
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
<script setup lang="ts">
|
|
2
|
+
import { computed } from 'vue';
|
|
2
3
|
import { Button, Notice } from '@eldrajs/ui';
|
|
3
4
|
import { useI18n } from 'vue-i18n';
|
|
4
5
|
import { useStorefront } from '../../../app/composables/useStorefront';
|
|
@@ -11,6 +12,10 @@ const cart = storefront.cart;
|
|
|
11
12
|
const feedback = useStorefrontFeedback();
|
|
12
13
|
const { t } = useI18n();
|
|
13
14
|
const sentence = useNotSoldSentence(cart);
|
|
15
|
+
const shopInDefault = computed(() => {
|
|
16
|
+
const fallback = storefront.currency.defaultCurrency;
|
|
17
|
+
return fallback !== undefined && cart.currency.value !== fallback;
|
|
18
|
+
});
|
|
14
19
|
|
|
15
20
|
let leaving = false;
|
|
16
21
|
|
|
@@ -56,13 +61,13 @@ async function removeThenCheckout(): Promise<void> {
|
|
|
56
61
|
{{ t('currency.checkoutWithout') }}
|
|
57
62
|
</Button>
|
|
58
63
|
<Button
|
|
59
|
-
v-if="
|
|
64
|
+
v-if="shopInDefault"
|
|
60
65
|
variant="outline"
|
|
61
66
|
size="sm"
|
|
62
67
|
type="button"
|
|
63
68
|
@click="storefront.currency.chooseBase()"
|
|
64
69
|
>
|
|
65
|
-
{{ t('currency.shopIn', { currency: storefront.currency.
|
|
70
|
+
{{ t('currency.shopIn', { currency: storefront.currency.defaultCurrency }) }}
|
|
66
71
|
</Button>
|
|
67
72
|
</template>
|
|
68
73
|
</Notice>
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
/** Header (design spec 2); apiId stays `navigation`. Layout: docs/starter-kit.md, "The header". */
|
|
3
3
|
import { computed, onMounted, ref, watchEffect } from 'vue';
|
|
4
4
|
import { useEldraLink, useEldraLocale } from '@eldrajs/theme-vue';
|
|
5
|
+
import type { ResolvedLink, ResolvedSection } from '@eldrajs/theme-vue';
|
|
5
6
|
import {
|
|
6
7
|
Badge,
|
|
7
8
|
Button,
|
|
@@ -15,7 +16,9 @@ import {
|
|
|
15
16
|
} from '@eldrajs/ui';
|
|
16
17
|
import type {
|
|
17
18
|
NavMenuItem,
|
|
19
|
+
NavMenuLink,
|
|
18
20
|
NavMenuPart,
|
|
21
|
+
NavMenuSection,
|
|
19
22
|
SearchResultItem,
|
|
20
23
|
SearchResults,
|
|
21
24
|
UiMessages,
|
|
@@ -31,6 +34,7 @@ import { isInternalHref, SEARCH_PATH } from '../../app/utils/links';
|
|
|
31
34
|
import { useMoney } from '../../app/storefront/money';
|
|
32
35
|
import type { StorefrontSearchResponse } from '../../app/storefront/types';
|
|
33
36
|
import MenuLink from './parts/MenuLink.vue';
|
|
37
|
+
import blockJson from './block.json';
|
|
34
38
|
|
|
35
39
|
const props = defineProps<{ entry: EldraBlockEntry<'navigation'> }>();
|
|
36
40
|
const { data } = useBlockData(props, 'navigation');
|
|
@@ -65,11 +69,50 @@ const links = computed(() =>
|
|
|
65
69
|
})
|
|
66
70
|
);
|
|
67
71
|
|
|
68
|
-
/**
|
|
72
|
+
/**
|
|
73
|
+
* The picture crop of a menu card: the `aspect` this block's own `links` field declares in
|
|
74
|
+
* `cards`, so the declaration and the drawing cannot drift. `NavMenu` falls back to 4:3 on a
|
|
75
|
+
* value that is not `width:height`.
|
|
76
|
+
*/
|
|
77
|
+
const cardAspect = (
|
|
78
|
+
blockJson.fields.find((field) => field.fieldId === 'links') as
|
|
79
|
+
| { metadata?: { item?: { metadata?: { tree?: { cards?: { aspect?: string } } } } } }
|
|
80
|
+
| undefined
|
|
81
|
+
)?.metadata?.item?.metadata?.tree?.cards?.aspect;
|
|
82
|
+
|
|
83
|
+
/** A resolved link as the menu draws it, `display` and `card` (child and section links only) kept. */
|
|
84
|
+
function toNavLink(link: ResolvedLink): NavMenuLink {
|
|
85
|
+
return {
|
|
86
|
+
label: link.label,
|
|
87
|
+
href: link.href,
|
|
88
|
+
newTab: link.newTab,
|
|
89
|
+
group: link.group,
|
|
90
|
+
current: isCurrent(link.href),
|
|
91
|
+
display: link.display,
|
|
92
|
+
card: link.card,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function toNavSection(section: ResolvedSection): NavMenuSection {
|
|
97
|
+
return {
|
|
98
|
+
heading: section.heading,
|
|
99
|
+
headingLink: section.headingLink === null ? null : toNavLink(section.headingLink),
|
|
100
|
+
display: section.display,
|
|
101
|
+
links: section.links.map(toNavLink),
|
|
102
|
+
seeAll: section.seeAll === null ? null : toNavLink(section.seeAll),
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* A parent with a panel is a disclosure, so its own destination moves to the panel's "View all". The
|
|
108
|
+
* panel is its `sections` when the site served them (this block declares them), else its `children`
|
|
109
|
+
* grouped by their `group`, which is what a menu stored before sections existed still is.
|
|
110
|
+
*/
|
|
69
111
|
const navItems = computed<NavMenuItem[]>(() =>
|
|
70
112
|
links.value.map((link) => {
|
|
113
|
+
const sections = link.sections ?? [];
|
|
71
114
|
const children = link.children.filter((child) => child.label !== null);
|
|
72
|
-
if (children.length === 0) {
|
|
115
|
+
if (sections.length === 0 && children.length === 0) {
|
|
73
116
|
return {
|
|
74
117
|
label: link.label,
|
|
75
118
|
href: link.href,
|
|
@@ -80,13 +123,9 @@ const navItems = computed<NavMenuItem[]>(() =>
|
|
|
80
123
|
return {
|
|
81
124
|
label: link.label,
|
|
82
125
|
layout: 'mega',
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
newTab: child.newTab,
|
|
87
|
-
group: child.group,
|
|
88
|
-
current: isCurrent(child.href),
|
|
89
|
-
})),
|
|
126
|
+
...(sections.length > 0
|
|
127
|
+
? { sections: sections.map(toNavSection) }
|
|
128
|
+
: { children: children.map(toNavLink) }),
|
|
90
129
|
};
|
|
91
130
|
})
|
|
92
131
|
);
|
|
@@ -668,8 +707,11 @@ const navMenuClasses = computed<Partial<Record<NavMenuPart, string>>>(() => ({
|
|
|
668
707
|
panel: 'pt-0',
|
|
669
708
|
content:
|
|
670
709
|
'rounded-none rounded-b-lg border-t-0 bg-(--eldra-header-surface) shadow-(--eldra-shadow-float) @wide:p-8',
|
|
671
|
-
columns
|
|
672
|
-
|
|
710
|
+
// Twelve columns, a link column three of them. A panel with a cards column is laid out in equal
|
|
711
|
+
// tracks instead: a link column is one, the cards column spans two (the design's ~1.7x).
|
|
712
|
+
columns:
|
|
713
|
+
'grid-cols-12 gap-6 data-cards:grid-flow-col data-cards:auto-cols-fr data-cards:grid-cols-none',
|
|
714
|
+
group: 'col-span-3 [[data-cards]>&]:col-span-1 [[data-cards]>&[data-cards]]:col-span-2',
|
|
673
715
|
footer: 'justify-end',
|
|
674
716
|
drawerLink: 'font-heading text-xl font-semibold',
|
|
675
717
|
drawerTrigger: 'font-heading text-xl font-semibold',
|
|
@@ -730,6 +772,7 @@ const navMenuClasses = computed<Partial<Record<NavMenuPart, string>>>(() => ({
|
|
|
730
772
|
:collapse-below="variant === 'minimal' ? 'always' : '@content'"
|
|
731
773
|
open-on-hover
|
|
732
774
|
:link-as="MenuLink"
|
|
775
|
+
:card-aspect="cardAspect"
|
|
733
776
|
:messages="navMenuMessages"
|
|
734
777
|
class="static contents"
|
|
735
778
|
:classes="navMenuClasses"
|
|
@@ -35,7 +35,14 @@ interface SeedTarget {
|
|
|
35
35
|
slug?: string;
|
|
36
36
|
id?: string;
|
|
37
37
|
}
|
|
38
|
+
interface SeedSection {
|
|
39
|
+
heading?: string;
|
|
40
|
+
links?: SeedLink[];
|
|
41
|
+
seeAll?: SeedLink;
|
|
42
|
+
headingLink?: SeedLink;
|
|
43
|
+
}
|
|
38
44
|
interface SeedLink {
|
|
45
|
+
sections?: SeedSection[];
|
|
39
46
|
kind: string;
|
|
40
47
|
target?: SeedTarget;
|
|
41
48
|
url?: string;
|
|
@@ -53,16 +60,22 @@ function resolveTargets<T extends Record<string, unknown>>(
|
|
|
53
60
|
const targets = new Map<string, unknown>();
|
|
54
61
|
const rewrite = (link: SeedLink): SeedLink => {
|
|
55
62
|
const slug = link.target?.slug;
|
|
56
|
-
|
|
57
|
-
return { ...link, ...(link.children ? { children: link.children.map(rewrite) } : {}) };
|
|
58
|
-
}
|
|
59
|
-
const id = `id-${slug}`;
|
|
60
|
-
targets.set(`${link.target._type}:${id}`, { slug, title: link.label });
|
|
61
|
-
return {
|
|
62
|
-
...link,
|
|
63
|
-
target: { _type: link.target._type, id },
|
|
63
|
+
const nested = {
|
|
64
64
|
...(link.children ? { children: link.children.map(rewrite) } : {}),
|
|
65
|
+
...(link.sections
|
|
66
|
+
? {
|
|
67
|
+
sections: link.sections.map((section) => ({
|
|
68
|
+
...section,
|
|
69
|
+
...(section.links ? { links: section.links.map(rewrite) } : {}),
|
|
70
|
+
...(section.seeAll ? { seeAll: rewrite(section.seeAll) } : {}),
|
|
71
|
+
})),
|
|
72
|
+
}
|
|
73
|
+
: {}),
|
|
65
74
|
};
|
|
75
|
+
if (link.target === undefined || slug === undefined) return { ...link, ...nested };
|
|
76
|
+
const id = `id-${slug}`;
|
|
77
|
+
targets.set(`${link.target._type}:${id}`, { slug, title: link.label });
|
|
78
|
+
return { ...link, target: { _type: link.target._type, id }, ...nested };
|
|
66
79
|
};
|
|
67
80
|
const next = { ...data } as Record<string, unknown>;
|
|
68
81
|
if (Array.isArray(next.links)) next.links = (next.links as SeedLink[]).map(rewrite);
|
|
@@ -277,6 +290,76 @@ describe('header block (navigation apiId)', () => {
|
|
|
277
290
|
wrapper.unmount();
|
|
278
291
|
});
|
|
279
292
|
|
|
293
|
+
it('draws a menu card with its picture at the declared aspect, its title and its price, axe-clean', async () => {
|
|
294
|
+
const targets = new Map(resolved.links.targets);
|
|
295
|
+
targets.set('product:p-1', {
|
|
296
|
+
slug: 'ash-mug',
|
|
297
|
+
title: 'Ash glaze mug',
|
|
298
|
+
image: 'https://media.test/mug-md.jpg',
|
|
299
|
+
price: 28,
|
|
300
|
+
});
|
|
301
|
+
targets.set('collection:c-1', {
|
|
302
|
+
slug: 'winter-knits',
|
|
303
|
+
title: 'Winter knits',
|
|
304
|
+
image: 'https://media.test/winter-md.jpg',
|
|
305
|
+
});
|
|
306
|
+
const wrapper = mount(Block, {
|
|
307
|
+
...mountOptions(
|
|
308
|
+
{
|
|
309
|
+
entry: {
|
|
310
|
+
id: 'e1',
|
|
311
|
+
data: {
|
|
312
|
+
...resolved.data,
|
|
313
|
+
links: [
|
|
314
|
+
{
|
|
315
|
+
kind: 'none',
|
|
316
|
+
label: 'Shop',
|
|
317
|
+
sections: [
|
|
318
|
+
{
|
|
319
|
+
heading: 'Featured',
|
|
320
|
+
display: 'card',
|
|
321
|
+
links: [
|
|
322
|
+
{ kind: 'product', target: { _type: 'product', id: 'p-1' } },
|
|
323
|
+
{ kind: 'collection', target: { _type: 'collection', id: 'c-1' } },
|
|
324
|
+
],
|
|
325
|
+
},
|
|
326
|
+
{
|
|
327
|
+
heading: 'Care',
|
|
328
|
+
links: [{ kind: 'url', url: '/care', label: 'Care guides' }],
|
|
329
|
+
},
|
|
330
|
+
],
|
|
331
|
+
},
|
|
332
|
+
],
|
|
333
|
+
},
|
|
334
|
+
},
|
|
335
|
+
},
|
|
336
|
+
{ links: { ...resolved.links, targets } }
|
|
337
|
+
),
|
|
338
|
+
attachTo: document.body,
|
|
339
|
+
});
|
|
340
|
+
const trigger = wrapper.findAll('button').find((b) => b.text().includes('Shop'))!;
|
|
341
|
+
await trigger.trigger('click');
|
|
342
|
+
const panel = wrapper.get(`#${trigger.attributes('aria-controls')}`);
|
|
343
|
+
const cards = panel.findAll('[data-part="card"]');
|
|
344
|
+
expect(cards).toHaveLength(2);
|
|
345
|
+
const image = cards[0]!.get('img');
|
|
346
|
+
expect(image.attributes('src')).toBe('https://media.test/mug-md.jpg');
|
|
347
|
+
expect((image.element as HTMLImageElement).style.aspectRatio).toBe('4 / 3');
|
|
348
|
+
expect(cards[0]!.get('[data-part="cardTitle"]').text()).toBe('Ash glaze mug');
|
|
349
|
+
expect(cards[0]!.get('[data-part="cardPrice"]').text()).toMatch(/28/);
|
|
350
|
+
expect(cards[1]!.get('img').attributes('src')).toBe('https://media.test/winter-md.jpg');
|
|
351
|
+
expect(cards[1]!.find('[data-part="cardPrice"]').exists()).toBe(false);
|
|
352
|
+
// The cards column is the wide one; the link column is one track.
|
|
353
|
+
const columns = panel.findAll('[data-part="group"]');
|
|
354
|
+
expect(columns[0]!.attributes('data-cards')).toBeDefined();
|
|
355
|
+
expect(columns[0]!.classes()).toContain('[[data-cards]>&[data-cards]]:col-span-2');
|
|
356
|
+
expect(columns[1]!.attributes('data-cards')).toBeUndefined();
|
|
357
|
+
// A plain link beside the cards is still a link.
|
|
358
|
+
expect(panel.findAll('a').some((a) => a.text() === 'Care guides')).toBe(true);
|
|
359
|
+
expect(await axe(wrapper.element)).toHaveNoViolations();
|
|
360
|
+
wrapper.unmount();
|
|
361
|
+
});
|
|
362
|
+
|
|
280
363
|
it('renders a heading-only mega-menu parent as a disclosure with no destination', async () => {
|
|
281
364
|
// `kind: "none"`: the trigger has a label and children and goes nowhere,
|
|
282
365
|
// which is what a mega-menu parent without its own page is.
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"description": "The store's top bar: brand, primary links with optional mega-menus, search, cart and one call to action.",
|
|
5
5
|
"icon": "layout-navbar",
|
|
6
6
|
"category": "structure",
|
|
7
|
-
"version":
|
|
7
|
+
"version": 5,
|
|
8
8
|
"groups": [
|
|
9
9
|
{
|
|
10
10
|
"groupId": "brand",
|
|
@@ -55,6 +55,10 @@
|
|
|
55
55
|
"label": "ctaLabel"
|
|
56
56
|
}
|
|
57
57
|
]
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
"version": 5,
|
|
61
|
+
"groupsToSections": [{ "field": "links" }]
|
|
58
62
|
}
|
|
59
63
|
],
|
|
60
64
|
"fields": [
|
|
@@ -99,7 +103,7 @@
|
|
|
99
103
|
"name": "Links",
|
|
100
104
|
"type": "list",
|
|
101
105
|
"groupId": "navigation",
|
|
102
|
-
"helpText": "Primary links. 3 to 6 recommended; more than 6 may overflow at 64–80rem. Give a link
|
|
106
|
+
"helpText": "Primary links. 3 to 6 recommended; more than 6 may overflow at 64–80rem. Give a link a panel to turn it into a mega-menu: up to 4 columns, each with a heading, its links and a See all link. A column can hold up to 2 product or collection cards, a picture with a title and a price.",
|
|
103
107
|
"metadata": {
|
|
104
108
|
"item": {
|
|
105
109
|
"fieldId": "link",
|
|
@@ -107,7 +111,11 @@
|
|
|
107
111
|
"type": "link",
|
|
108
112
|
"localized": true,
|
|
109
113
|
"metadata": {
|
|
110
|
-
"tree":
|
|
114
|
+
"tree": {
|
|
115
|
+
"depth": 2,
|
|
116
|
+
"sections": { "max": 4, "headingLink": true, "seeAll": true },
|
|
117
|
+
"cards": { "kinds": ["product", "collection"], "max": 2, "aspect": "4:3" }
|
|
118
|
+
},
|
|
111
119
|
"kinds": ["product", "collection", "category", "page", "entry", "url", "none"]
|
|
112
120
|
}
|
|
113
121
|
}
|