@usequeek/theme-kit 0.1.34 → 0.1.36

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/apps/apps.css CHANGED
@@ -61,10 +61,42 @@
61
61
  max-width: 400px;
62
62
  }
63
63
 
64
+ /* Following: a one-line state with the way out. */
65
+ .core-follow-card--compact {
66
+ display: flex;
67
+ align-items: center;
68
+ gap: 0.75rem;
69
+ }
70
+ .core-follow-card--compact.core-follow-card--floating {
71
+ width: auto;
72
+ padding: 0.5rem 0.75rem;
73
+ }
74
+ .core-follow-card--compact.core-follow-card--inline {
75
+ padding: 0.75rem 1.25rem;
76
+ }
77
+ .core-follow-card__state {
78
+ font-size: 0.8rem;
79
+ font-weight: 600;
80
+ }
81
+ .core-follow-card__unfollow {
82
+ background: none;
83
+ border: 0;
84
+ padding: 0;
85
+ font: inherit;
86
+ font-size: 0.8rem;
87
+ color: var(--brand-text-muted, #666);
88
+ text-decoration: underline;
89
+ cursor: pointer;
90
+ }
91
+ .core-follow-card__unfollow:disabled {
92
+ opacity: 0.6;
93
+ cursor: not-allowed;
94
+ }
95
+
64
96
  .core-follow-card__dismiss {
65
97
  position: absolute;
66
98
  top: 0.5rem;
67
- right: 0.5rem;
99
+ inset-inline-end: 0.5rem;
68
100
  width: 24px;
69
101
  height: 24px;
70
102
  border: 0;
@@ -173,6 +205,12 @@
173
205
  width: auto;
174
206
  bottom: 5rem;
175
207
  }
208
+ /* The compact following state stays a small chip at the right edge. */
209
+ .core-follow-card--compact.core-follow-card--floating {
210
+ inset-inline-start: auto;
211
+ inset-inline-end: 1rem;
212
+ width: auto;
213
+ }
176
214
  /* Reset position overrides on mobile — always full-width bottom */
177
215
  .core-follow-card--bottom-left,
178
216
  .core-follow-card--center-right,
@@ -1,8 +1,8 @@
1
1
  'use client';
2
2
 
3
3
  import type { JSX } from 'react';
4
- import { useState, useCallback, useEffect } from 'react';
5
- import { useStorefront } from '../provider';
4
+ import { useState, useCallback, useEffect, useSyncExternalStore } from 'react';
5
+ import { useStorefront, useStorefrontLocale, useThemeStrings } from '../provider';
6
6
  import { useUserStore } from '../stores/user-store';
7
7
  import { useAuthModalStore } from '../stores/auth-modal-store';
8
8
  import { getQueekClient } from '../sdk/queek-client';
@@ -42,26 +42,71 @@ const POSITION_CLASS: Record<string, string> = {
42
42
  };
43
43
 
44
44
  /**
45
- * The store's follow card, shown when the merchant has turned it on. It is
46
- * floating by default; `display="inline"` places it in the page flow. Renders
47
- * nothing when the app is off.
45
+ * Where the merchant put the card: `footer` is the inline mount a theme places in its footer, anything else
46
+ * (`floating`, and the retired `block`) is the floating mount the shell places.
47
+ */
48
+ function settingFor(display: string | undefined): 'floating' | 'footer' {
49
+ return display === 'footer' ? 'footer' : 'floating';
50
+ }
51
+
52
+ /**
53
+ * The inline mounts currently rendered by a theme, so the floating mount can tell that a theme has no footer mount
54
+ * and take the card itself: a card the merchant switched on is always reachable, and never shown twice.
55
+ */
56
+ let inlineMounts = 0;
57
+ const mountListeners = new Set<() => void>();
58
+
59
+ function subscribeToMounts(listener: () => void): () => void {
60
+ mountListeners.add(listener);
61
+ return () => {
62
+ mountListeners.delete(listener);
63
+ };
64
+ }
65
+
66
+ function registerInlineMount(): () => void {
67
+ inlineMounts += 1;
68
+ mountListeners.forEach((notify) => notify());
69
+ return () => {
70
+ inlineMounts -= 1;
71
+ mountListeners.forEach((notify) => notify());
72
+ };
73
+ }
74
+
75
+ /**
76
+ * The store's follow card, shown when the merchant has turned it on, in the one place they chose: the shell mounts
77
+ * `display="floating"` and a theme's footer mounts `display="inline"`; each renders only when the merchant's setting
78
+ * matches its mount, and a theme with no footer mount shows the floating card instead. Renders nothing when the app
79
+ * is off.
80
+ *
81
+ * Sharing contact details is the shopper's own choice: the box starts unticked and nothing is sent unless it is ticked.
82
+ * A shopper who follows sees a compact "Following · Unfollow" state instead of the prompt.
48
83
  */
49
84
  export function FollowCard({ display }: { display?: 'floating' | 'inline' } = {}): JSX.Element | null {
50
85
  const { config, vendor } = useStorefront();
86
+ const t = useThemeStrings();
87
+ const locale = useStorefrontLocale();
51
88
  const followConfig = config.apps?.follow_card;
52
- const resolvedDisplay = display ?? followConfig?.display ?? 'floating';
89
+ const mount = display === 'inline' ? 'inline' : 'floating';
90
+ const setting = settingFor(followConfig?.display);
91
+ const registersInline = mount === 'inline' && setting === 'footer' && !!followConfig?.enabled;
92
+ // The server answer is "a footer mount exists" so a footer-placed card is never painted floating before hydration.
93
+ const footerMountExists = useSyncExternalStore(subscribeToMounts, () => inlineMounts > 0, () => true);
94
+ const shows = mount === 'inline' ? setting === 'footer' : setting === 'floating' || !footerMountExists;
53
95
  const user = useUserStore((s) => s.user);
54
96
  const openAuth = useAuthModalStore((s) => s.open);
55
97
 
56
98
  const [isFollowing, setIsFollowing] = useState(false);
57
99
  const [followersCount, setFollowersCount] = useState(config.followers_count ?? 0);
58
- const [shareContact, setShareContact] = useState(true);
100
+ const [shareContact, setShareContact] = useState(false);
59
101
  const [loading, setLoading] = useState(false);
60
102
  const [dismissed, setDismissed] = useState(false);
61
103
  const [checked, setChecked] = useState(false);
62
104
 
105
+ useEffect(() => (registersInline ? registerInlineMount() : undefined), [registersInline]);
106
+
63
107
  // Check follow status when user is logged in
64
108
  useEffect(() => {
109
+ if (!shows) return;
65
110
  if (!user || !vendor.slug) {
66
111
  setChecked(true);
67
112
  return;
@@ -74,7 +119,7 @@ export function FollowCard({ display }: { display?: 'floating' | 'inline' } = {}
74
119
  })
75
120
  .catch(() => {})
76
121
  .finally(() => setChecked(true));
77
- }, [user, vendor.slug]);
122
+ }, [user, vendor.slug, shows]);
78
123
 
79
124
  // Check persistent dismiss
80
125
  useEffect(() => {
@@ -92,8 +137,9 @@ export function FollowCard({ display }: { display?: 'floating' | 'inline' } = {}
92
137
  setLoading(true);
93
138
  try {
94
139
  const client = getQueekClient(vendor.slug ?? undefined);
140
+ // `contact_opt_in` is the only consent the API reads; the older `share_contact` is ignored as not consent.
95
141
  const res = await client.post<FollowData>('/client/store/follow', {
96
- share_contact: shareContact,
142
+ contact_opt_in: shareContact,
97
143
  });
98
144
  setIsFollowing(res.data?.following ?? true);
99
145
  setFollowersCount(res.data?.followers_count ?? followersCount + 1);
@@ -125,28 +171,42 @@ export function FollowCard({ display }: { display?: 'floating' | 'inline' } = {}
125
171
  if (vendor.id) persistDismiss(vendor.id);
126
172
  }, [vendor.id]);
127
173
 
128
- if (!followConfig?.enabled || dismissed || !checked) return null;
129
- // Don't show to users already following
130
- if (isFollowing) return null;
174
+ if (!followConfig?.enabled || !shows || !checked) return null;
131
175
 
132
- const isFloating = resolvedDisplay === 'floating';
176
+ const storeName = vendor.name ?? t('follow.thisstore');
177
+ const isFloating = mount === 'floating';
133
178
  const posClass = isFloating
134
179
  ? POSITION_CLASS[followConfig.position ?? 'bottom-right'] ?? POSITION_CLASS['bottom-right']
135
180
  : '';
181
+ const placement = isFloating ? `core-follow-card--floating ${posClass}` : 'core-follow-card--inline';
182
+
183
+ // A shopper who follows still sees the card, compact, with the way out: Unfollow. Dismissing the prompt never hides it.
184
+ if (isFollowing) {
185
+ return (
186
+ <div className={`core-follow-card core-follow-card--compact ${placement}`} role="complementary" aria-label={t('follow.following')}>
187
+ <span className="core-follow-card__state">{t('follow.following')}</span>
188
+ <button type="button" className="core-follow-card__unfollow" onClick={handleUnfollow} disabled={loading}>
189
+ {loading ? t('follow.leaving') : t('follow.unfollow')}
190
+ </button>
191
+ </div>
192
+ );
193
+ }
194
+
195
+ if (dismissed) return null;
136
196
 
137
197
  return (
138
198
  <div
139
- className={`core-follow-card ${isFloating ? `core-follow-card--floating ${posClass}` : 'core-follow-card--inline'}`}
199
+ className={`core-follow-card ${placement}`}
140
200
  role="complementary"
141
- aria-label="Follow this store"
201
+ aria-label={t('follow.aria')}
142
202
  >
143
203
  {isFloating && (
144
204
  <button
145
205
  type="button"
146
206
  className="core-follow-card__dismiss"
147
207
  onClick={handleDismiss}
148
- aria-label="Don't show again"
149
- title="Don't show again"
208
+ aria-label={t('follow.dismiss')}
209
+ title={t('follow.dismiss')}
150
210
  >
151
211
  <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" width="12" height="12">
152
212
  <path d="M3 3l10 10M13 3L3 13" />
@@ -164,14 +224,14 @@ export function FollowCard({ display }: { display?: 'floating' | 'inline' } = {}
164
224
  )}
165
225
  <div className="core-follow-card__text">
166
226
  <p className="core-follow-card__title">
167
- Follow {vendor.name ?? 'this store'}
227
+ {t('follow.title', { name: storeName })}
168
228
  </p>
169
229
  <p className="core-follow-card__sub">
170
- Get notified about new arrivals and exclusive offers.
230
+ {t('follow.tagline')}
171
231
  </p>
172
232
  {followersCount > 0 && (
173
233
  <p className="core-follow-card__count">
174
- {formatCount(followersCount)} {followersCount === 1 ? 'follower' : 'followers'}
234
+ {t('follow.count', { count: followersCount, n: formatCount(followersCount, locale) })}
175
235
  </p>
176
236
  )}
177
237
  </div>
@@ -184,7 +244,7 @@ export function FollowCard({ display }: { display?: 'floating' | 'inline' } = {}
184
244
  onChange={(e) => setShareContact(e.target.checked)}
185
245
  className="core-follow-card__checkbox"
186
246
  />
187
- <span>Share my contact info with this store</span>
247
+ <span>{t('follow.share', { name: storeName })}</span>
188
248
  </label>
189
249
  <button
190
250
  type="button"
@@ -192,7 +252,7 @@ export function FollowCard({ display }: { display?: 'floating' | 'inline' } = {}
192
252
  onClick={handleFollow}
193
253
  disabled={loading}
194
254
  >
195
- {loading ? 'Following...' : 'Follow'}
255
+ {loading ? t('follow.busy') : t('follow.action')}
196
256
  </button>
197
257
  </div>
198
258
  );
@@ -185,5 +185,22 @@
185
185
  "market": {
186
186
  "label": "Currency",
187
187
  "named": "Currency: {name}"
188
+ },
189
+ "follow": {
190
+ "aria": "Follow this store",
191
+ "dismiss": "Don't show again",
192
+ "title": "Follow {name}",
193
+ "thisstore": "this store",
194
+ "tagline": "Get notified about new arrivals and exclusive offers.",
195
+ "count": {
196
+ "one": "{n} follower",
197
+ "other": "{n} followers"
198
+ },
199
+ "share": "Optional: share my name, email and phone number with {name}, so they can contact me.",
200
+ "action": "Follow",
201
+ "busy": "Following…",
202
+ "following": "Following",
203
+ "unfollow": "Unfollow",
204
+ "leaving": "Unfollowing…"
188
205
  }
189
206
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@usequeek/theme-kit",
3
- "version": "0.1.34",
3
+ "version": "0.1.36",
4
4
  "private": false,
5
5
  "license": "MIT",
6
6
  "description": "The runtime a Queek storefront theme is built on: hooks, stores, page renderer, navigation, shared blocks and locale strings.",
@@ -90,6 +90,12 @@ export function getLastRequestId(): string | null {
90
90
 
91
91
  let cachedClient: QueekClientInstance | null = null;
92
92
  let cachedSlug: string | null = null;
93
+ let cachedTokens: string | null = null;
94
+
95
+ /** The stored token pair, as one comparable value. */
96
+ function storedTokens(): string {
97
+ return `${localStorageAdapter.getItem(ACCESS_KEY) ?? ''}\n${localStorageAdapter.getItem(REFRESH_KEY) ?? ''}`;
98
+ }
93
99
 
94
100
  /**
95
101
  * The Queek customer SDK client for this store, created once and reused.
@@ -97,14 +103,22 @@ let cachedSlug: string | null = null;
97
103
  * every request to the same-origin `/api/v1`. A 401 signs the customer out and
98
104
  * opens the sign-in modal (see `triggerReauth`).
99
105
  *
106
+ * The SDK reads the tokens once, when the client is created, so the cached
107
+ * client is rebuilt whenever the stored pair changed under it. Another tab
108
+ * signing in writes the tokens here too, and the user store syncs across tabs:
109
+ * a client still holding no token would send a signed-in shopper's request
110
+ * bare, its 401 would sign them out, and that sign-out would sync back to the
111
+ * tab they had just signed in on.
112
+ *
100
113
  * @example
101
114
  * const client = getQueekClient(vendor.slug);
102
115
  * const orders = await client.get('/client/store/orders');
103
116
  */
104
117
  export function getQueekClient(vendorSlug?: string): QueekClientInstance {
105
118
  const slug = vendorSlug ?? null;
119
+ const tokens = storedTokens();
106
120
 
107
- if (cachedClient && cachedSlug === slug) {
121
+ if (cachedClient && cachedSlug === slug && cachedTokens === tokens) {
108
122
  return cachedClient;
109
123
  }
110
124
 
@@ -121,6 +135,7 @@ export function getQueekClient(vendorSlug?: string): QueekClientInstance {
121
135
  },
122
136
  });
123
137
  cachedSlug = slug;
138
+ cachedTokens = tokens;
124
139
 
125
140
  return cachedClient;
126
141
  }
@@ -155,4 +170,5 @@ export function setAuthTokens(accessToken: string, refreshToken: string): void {
155
170
  // Reset cache so the next getQueekClient() reads the new tokens from localStorage.
156
171
  cachedClient = null;
157
172
  cachedSlug = null;
173
+ cachedTokens = null;
158
174
  }