fontdue-js 3.15.0 → 3.16.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.
Files changed (35) hide show
  1. package/AGENTS.md +29 -0
  2. package/CHANGELOG.md +10 -0
  3. package/dist/__generated__/FreeDownloadFormMutation.graphql.d.ts +43 -0
  4. package/dist/__generated__/FreeDownloadFormMutation.graphql.js +98 -0
  5. package/dist/__generated__/FreeDownloadForm_Query.graphql.d.ts +46 -0
  6. package/dist/__generated__/FreeDownloadForm_Query.graphql.js +186 -0
  7. package/dist/__tests__/freeDownloadForm.test.js +222 -0
  8. package/dist/__tests__/i18nComponents.test.js +1 -1
  9. package/dist/__tests__/trackEvents.test.js +131 -0
  10. package/dist/__tests__/trackingPageViews.test.js +339 -0
  11. package/dist/__tests__/trackingRouteChanges.test.js +48 -2
  12. package/dist/components/FreeDownloadForm/FreeDownloadFormElement.d.ts +10 -0
  13. package/dist/components/FreeDownloadForm/FreeDownloadFormElement.js +17 -0
  14. package/dist/components/FreeDownloadForm/index.d.ts +21 -0
  15. package/dist/components/FreeDownloadForm/index.js +283 -0
  16. package/dist/components/FreeDownloadForm/index.server.d.ts +9 -0
  17. package/dist/components/FreeDownloadForm/index.server.js +22 -0
  18. package/dist/components/FreeDownloadForm/variables.d.ts +8 -0
  19. package/dist/components/FreeDownloadForm/variables.js +18 -0
  20. package/dist/components/Root/index.js +1 -0
  21. package/dist/components/Tracking/index.js +52 -34
  22. package/dist/components/Tracking/pageViews.d.ts +26 -0
  23. package/dist/components/Tracking/pageViews.js +94 -0
  24. package/dist/components/Tracking/routeChanges.d.ts +9 -1
  25. package/dist/components/Tracking/routeChanges.js +33 -9
  26. package/dist/components/Tracking/sendTrackEvent.d.ts +28 -0
  27. package/dist/components/Tracking/sendTrackEvent.js +68 -0
  28. package/dist/components/track.d.ts +21 -0
  29. package/dist/components/track.js +31 -0
  30. package/dist/fontdue.css +32 -0
  31. package/dist/i18n/de.js +17 -5
  32. package/dist/i18n/en.d.ts +11 -0
  33. package/dist/i18n/en.js +12 -0
  34. package/dist/version.js +1 -1
  35. package/package.json +6 -1
@@ -17,16 +17,20 @@ export function trackRouteChanges(_ref) {
17
17
  window: windowLike,
18
18
  currentUrl,
19
19
  onNavigate,
20
+ claim = lastUrlClaim(currentUrl()),
20
21
  defer = callback => {
21
22
  setTimeout(callback, 0);
22
23
  }
23
24
  } = _ref;
24
- // The caller has already reported the URL we start on, so it is the first
25
- // thing we compare against.
26
- let lastUrl = currentUrl();
25
+ // Set on cleanup. A check deferred before then must not fire afterwards: a
26
+ // provider that unmounts with the navigation it announced would otherwise
27
+ // report it from beyond the grave while its replacement reports it too.
28
+ let stopped = false;
27
29
  const handleRouteChange = () => {
30
+ if (stopped) return;
28
31
  // Defer slightly to let document.title update
29
32
  defer(() => {
33
+ if (stopped) return;
30
34
  const url = currentUrl();
31
35
 
32
36
  // A history entry gets rewritten in place for all sorts of reasons that
@@ -38,25 +42,45 @@ export function trackRouteChanges(_ref) {
38
42
  // URL that actually differs from the last one we reported can be a new
39
43
  // page view. Note this compares against the *previous* URL rather than
40
44
  // every URL seen, so A → B → A is three page views, as it should be.
41
- if (url === lastUrl) return;
42
- lastUrl = url;
45
+ if (!claim(url)) return;
43
46
  onNavigate();
44
47
  });
45
48
  };
46
49
  const originalPushState = history.pushState.bind(history);
47
50
  const originalReplaceState = history.replaceState.bind(history);
48
- history.pushState = function () {
51
+ const wrappedPushState = function () {
49
52
  originalPushState(...arguments);
50
53
  handleRouteChange();
51
54
  };
52
- history.replaceState = function () {
55
+ const wrappedReplaceState = function () {
53
56
  originalReplaceState(...arguments);
54
57
  handleRouteChange();
55
58
  };
59
+ history.pushState = wrappedPushState;
60
+ history.replaceState = wrappedReplaceState;
56
61
  windowLike.addEventListener('popstate', handleRouteChange);
57
62
  return () => {
58
- history.pushState = originalPushState;
59
- history.replaceState = originalReplaceState;
63
+ stopped = true;
64
+ // Put the originals back only while our wrappers are still the outermost.
65
+ // If something wrapped history after us (a router patching it, say),
66
+ // restoring would silently remove its wrapper too; ours stays in its chain
67
+ // instead, passing calls straight through now that it is stopped.
68
+ if (history.pushState === wrappedPushState) history.pushState = originalPushState;
69
+ if (history.replaceState === wrappedReplaceState) {
70
+ history.replaceState = originalReplaceState;
71
+ }
60
72
  windowLike.removeEventListener('popstate', handleRouteChange);
61
73
  };
74
+ }
75
+
76
+ // The default `claim`: a URL is new when it differs from the last one claimed.
77
+ // The caller has already reported the URL we start on, so it is the first thing
78
+ // we compare against.
79
+ function lastUrlClaim(startUrl) {
80
+ let lastUrl = startUrl;
81
+ return url => {
82
+ if (url === lastUrl) return false;
83
+ lastUrl = url;
84
+ return true;
85
+ };
62
86
  }
@@ -0,0 +1,28 @@
1
+ export type TrackProperties = Record<string, unknown>;
2
+ export interface TrackOptions {
3
+ /**
4
+ * The customer the event is about, as their Fontdue customer ID (the `id`
5
+ * of `viewer.currentCustomer`). For a page that knows who the customer is
6
+ * from its own server – a page shown in the desktop app, which receives the
7
+ * customer's access token – without them having signed in on the site.
8
+ * A visitor signed in through fontdue.js is identified without it.
9
+ */
10
+ userId?: string;
11
+ }
12
+ /**
13
+ * Records one of the foundry's own analytics events through the tracking API.
14
+ *
15
+ * Everything the page's own page views carry rides along: the visitor's
16
+ * anonymous ID (the cookieless one while consent is pending), the signed-in
17
+ * customer's access token, and the page the event happened on. The server
18
+ * stores the event and forwards it to the foundry's destinations when the
19
+ * visitor's consent allows, exactly like a page view.
20
+ *
21
+ * Never throws: tracking must not break the page. A request the server
22
+ * refuses – a name Fontdue records itself, properties that aren't an object –
23
+ * is reported with `console.warn`, so the developer sees why.
24
+ *
25
+ * Sent with `keepalive`, so an event fired from a click that navigates away
26
+ * still arrives.
27
+ */
28
+ export declare function sendTrackEvent(url: string, event: string, properties?: TrackProperties, options?: TrackOptions): Promise<void>;
@@ -0,0 +1,68 @@
1
+ import { applyCustomerAuthHeader } from '../../auth/session.js';
2
+ import { getClientAnonymousId, hasConsent } from '../ConsentBanner/consent.js';
3
+ /**
4
+ * Records one of the foundry's own analytics events through the tracking API.
5
+ *
6
+ * Everything the page's own page views carry rides along: the visitor's
7
+ * anonymous ID (the cookieless one while consent is pending), the signed-in
8
+ * customer's access token, and the page the event happened on. The server
9
+ * stores the event and forwards it to the foundry's destinations when the
10
+ * visitor's consent allows, exactly like a page view.
11
+ *
12
+ * Never throws: tracking must not break the page. A request the server
13
+ * refuses – a name Fontdue records itself, properties that aren't an object –
14
+ * is reported with `console.warn`, so the developer sees why.
15
+ *
16
+ * Sent with `keepalive`, so an event fired from a click that navigates away
17
+ * still arrives.
18
+ */
19
+ export async function sendTrackEvent(url, event) {
20
+ let properties = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
21
+ let options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
22
+ if (typeof window === 'undefined') return;
23
+ if (!url) {
24
+ console.warn('fontdue track: no Fontdue URL – call it after Fontdue is initialized.');
25
+ return;
26
+ }
27
+ const body = {
28
+ event,
29
+ properties,
30
+ context: {
31
+ userAgent: navigator.userAgent,
32
+ page: {
33
+ url: window.location.href,
34
+ path: window.location.pathname,
35
+ title: document.title,
36
+ referrer: document.referrer,
37
+ search: window.location.search
38
+ }
39
+ }
40
+ };
41
+
42
+ // Only the cookieless ID needs sending: once consent is granted (or never
43
+ // required) the server's cookie is the visitor's ID. Same rule as page views.
44
+ const anonymousId = hasConsent('analytics') ? undefined : getClientAnonymousId();
45
+ if (anonymousId) body.anonymous_id = anonymousId;
46
+ if (options.userId) body.user_id = options.userId;
47
+ const headers = {
48
+ 'Content-Type': 'application/json'
49
+ };
50
+ // A signed-in visitor's token vouches for who they are. An expired one is
51
+ // harmless here: the server records the event without a customer.
52
+ applyCustomerAuthHeader(headers, url);
53
+ try {
54
+ const response = await fetch(`${url}/api/track`, {
55
+ method: 'POST',
56
+ headers,
57
+ credentials: 'include',
58
+ keepalive: true,
59
+ body: JSON.stringify(body)
60
+ });
61
+ if (!response.ok) {
62
+ const error = await response.json().then(json => json === null || json === void 0 ? void 0 : json.error).catch(() => undefined);
63
+ console.warn(`fontdue track: “${event}” was not recorded` + (typeof error === 'string' ? ` – ${error}` : ` (HTTP ${response.status})`));
64
+ }
65
+ } catch (error) {
66
+ console.debug('fontdue track: request failed', error);
67
+ }
68
+ }
@@ -0,0 +1,21 @@
1
+ import { type TrackOptions, type TrackProperties } from './Tracking/sendTrackEvent.js';
2
+ export type { TrackOptions, TrackProperties };
3
+ /**
4
+ * Returns a function that records one of your own analytics events, sent to
5
+ * the Fontdue site `<FontdueProvider>` is configured for.
6
+ *
7
+ * @example
8
+ * const track = useTrack();
9
+ * <button onClick={() => track('Contest Entered', { contest: 'summer' })}>Enter</button>
10
+ */
11
+ export declare function useTrack(): (event: string, properties?: TrackProperties, options?: TrackOptions) => Promise<void>;
12
+ /**
13
+ * Records one of your own analytics events from outside a React component,
14
+ * sent to the Fontdue site configured in the environment (`FONTDUE_URL`,
15
+ * `NEXT_PUBLIC_FONTDUE_URL`…). Inside a component, prefer `useTrack()`, which
16
+ * follows `<FontdueProvider url>`.
17
+ *
18
+ * @example
19
+ * track('Newsletter Link Clicked', { placement: 'footer' });
20
+ */
21
+ export declare function track(event: string, properties?: TrackProperties, options?: TrackOptions): Promise<void>;
@@ -0,0 +1,31 @@
1
+ 'use client';
2
+
3
+ import { useCallback } from 'react';
4
+ import { useFontdueUrl } from './UrlContext.js';
5
+ import { fontdueBaseUrl } from '../relay/environment.js';
6
+ import { sendTrackEvent } from './Tracking/sendTrackEvent.js';
7
+ /**
8
+ * Returns a function that records one of your own analytics events, sent to
9
+ * the Fontdue site `<FontdueProvider>` is configured for.
10
+ *
11
+ * @example
12
+ * const track = useTrack();
13
+ * <button onClick={() => track('Contest Entered', { contest: 'summer' })}>Enter</button>
14
+ */
15
+ export function useTrack() {
16
+ const url = useFontdueUrl();
17
+ return useCallback((event, properties, options) => sendTrackEvent(url, event, properties, options), [url]);
18
+ }
19
+
20
+ /**
21
+ * Records one of your own analytics events from outside a React component,
22
+ * sent to the Fontdue site configured in the environment (`FONTDUE_URL`,
23
+ * `NEXT_PUBLIC_FONTDUE_URL`…). Inside a component, prefer `useTrack()`, which
24
+ * follows `<FontdueProvider url>`.
25
+ *
26
+ * @example
27
+ * track('Newsletter Link Clicked', { placement: 'footer' });
28
+ */
29
+ export function track(event, properties, options) {
30
+ return sendTrackEvent(fontdueBaseUrl() ?? '', event, properties, options);
31
+ }
package/dist/fontdue.css CHANGED
@@ -116,6 +116,37 @@
116
116
  text-decoration: underline;
117
117
  }
118
118
 
119
+ .free-download__styles, .free-download__section {
120
+ margin-bottom: 30px;
121
+ }
122
+ .free-download__errors {
123
+ color: red;
124
+ margin-bottom: 30px;
125
+ }
126
+ .free-download__errors p {
127
+ margin: 0 0 0.5em;
128
+ }
129
+ .free-download__checkbox-field {
130
+ display: flex;
131
+ align-items: center;
132
+ margin-bottom: 20px;
133
+ }
134
+ .free-download__checkbox-field a {
135
+ color: inherit;
136
+ }
137
+ .free-download__checkbox-field .checkbox {
138
+ --checkbox-color: currentColor;
139
+ }
140
+ .free-download__checkbox-field label p {
141
+ margin: 0;
142
+ }
143
+ .free-download__checkbox-field[data-highlighted=true] {
144
+ color: red;
145
+ }
146
+ .free-download__required {
147
+ opacity: 0.6;
148
+ }
149
+
119
150
  .icon {
120
151
  display: inline-flex;
121
152
  justify-content: center;
@@ -4099,6 +4130,7 @@ input:disabled + .fontdue-admin-toolbar__switch {
4099
4130
  }
4100
4131
 
4101
4132
  fontdue-test-fonts-form,
4133
+ fontdue-free-download-form,
4102
4134
  fontdue-newsletter-signup,
4103
4135
  fontdue-customer-login-form,
4104
4136
  fontdue-customer-sign-in {
package/dist/i18n/de.js CHANGED
@@ -2,7 +2,7 @@
2
2
  // exactly the English keys: a missing or extra key fails `npm run build-ts`.
3
3
  //
4
4
  // Glossary – keep these consistent across the catalogue:
5
- // cart → Warenkorb; checkout → Kasse (button: „Zur Kasse“)
5
+ // cart → Warenkorb; checkout → Checkout (button to payment: „Weiter zur Zahlung“)
6
6
  // order → Bestellung; add to cart → „In den Warenkorb“
7
7
  // license → Lizenz; licensee → Lizenznehmer
8
8
  // style → Schnitt (Schnitte); font → Font (Fonts)
@@ -54,7 +54,7 @@ const de = {
54
54
  // TODO(proofread): the index of every typeface; „Alle Kollektionen“ is the literal.
55
55
  'storeModal.allCollections': 'Alle Schriften',
56
56
  'storeModal.buyingOptions': 'Kaufoptionen',
57
- 'storeModal.checkout': 'Kasse',
57
+ 'storeModal.checkout': 'Checkout',
58
58
  'storeModal.review': 'Überprüfen',
59
59
  // Follows the family's name: „Helvetica Familie“.
60
60
  // TODO(proofread): or hyphenated („Helvetica-Familie“), which the code can't do yet.
@@ -96,7 +96,7 @@ const de = {
96
96
  // to say plainly that the order costs money.
97
97
  'cart.submitPayment': 'Zahlungspflichtig bestellen',
98
98
  'cart.proceed': 'Weiter',
99
- 'cart.proceedToCheckout': 'Weiter zur Kasse',
99
+ 'cart.proceedToCheckout': 'Weiter zum Checkout',
100
100
  'cart.paymentRetry': 'Ihre Zahlung konnte nicht abgeschlossen werden. Bitte geben Sie Ihre Kartendaten erneut ein und versuchen Sie es noch einmal.',
101
101
  'cart.subtotal': 'Zwischensumme',
102
102
  'cart.discountApplied': 'Rabatt',
@@ -105,7 +105,7 @@ const de = {
105
105
  'cart.vat': 'USt.',
106
106
  'cart.taxNotApplicable': '{tax} entfällt',
107
107
  'cart.tax': 'Steuer',
108
- 'cart.calculatedAtCheckout': 'Wird an der Kasse berechnet',
108
+ 'cart.calculatedAtCheckout': 'Wird im Checkout berechnet',
109
109
  'cart.total': 'Gesamt',
110
110
  'cart.totalIncludingGst': 'Gesamt inkl. GST',
111
111
  'cart.empty': 'Ihr Warenkorb ist leer',
@@ -148,7 +148,7 @@ const de = {
148
148
  'checkout.paymentFailed': 'Die Zahlung ist fehlgeschlagen. Bitte versuchen Sie es mit einer anderen Zahlungsart.',
149
149
  'checkout.save': 'Speichern',
150
150
  'checkout.continue': 'Weiter',
151
- 'checkout.checkout': 'Zur Kasse',
151
+ 'checkout.checkout': 'Weiter zur Zahlung',
152
152
  'checkout.licenseeSelf': 'Sie selbst',
153
153
  // TODO(proofread): „Ihr Kunde“ is the masculine generic; „Ihre Kundschaft“ or
154
154
  // „Auftraggeber*in“ if the foundry prefers.
@@ -228,6 +228,18 @@ const de = {
228
228
  'testFonts.unavailable': 'Für diese Familie sind keine Testschriften verfügbar.',
229
229
  'testFonts.families': 'Familien',
230
230
  'testFonts.selectAll': 'Alle auswählen',
231
+ // Free download form
232
+ 'freeDownload.download': 'Herunterladen',
233
+ 'freeDownload.unavailable': 'Diese Schriften sind nicht als kostenloser Download verfügbar.',
234
+ 'freeDownload.freeStyles': 'Kostenlose Schnitte: {styles}',
235
+ 'freeDownload.agreement': {
236
+ one: 'Ich stimme der Lizenz {licenses} zu',
237
+ other: 'Ich stimme den Lizenzen {licenses} zu'
238
+ },
239
+ 'freeDownload.and': 'und',
240
+ 'freeDownload.newsletter': 'Für den Newsletter anmelden',
241
+ 'freeDownload.newsletterRequired': '(für den Download erforderlich)',
242
+ 'freeDownload.sent': 'Bitte prüfen Sie Ihr Postfach – wir haben den Download-Link an {email} gesendet.',
231
243
  // Password-protected collections
232
244
  'password.incorrect': 'Falsches Passwort',
233
245
  'password.label': 'Passwort *',
package/dist/i18n/en.d.ts CHANGED
@@ -170,6 +170,17 @@ declare const en: {
170
170
  'testFonts.unavailable': string;
171
171
  'testFonts.families': string;
172
172
  'testFonts.selectAll': string;
173
+ 'freeDownload.download': string;
174
+ 'freeDownload.unavailable': string;
175
+ 'freeDownload.freeStyles': string;
176
+ 'freeDownload.agreement': {
177
+ one: string;
178
+ other: string;
179
+ };
180
+ 'freeDownload.and': string;
181
+ 'freeDownload.newsletter': string;
182
+ 'freeDownload.newsletterRequired': string;
183
+ 'freeDownload.sent': string;
173
184
  'password.incorrect': string;
174
185
  'password.label': string;
175
186
  'password.placeholder': string;
package/dist/i18n/en.js CHANGED
@@ -208,6 +208,18 @@ const en = {
208
208
  'testFonts.unavailable': 'Test fonts aren’t available for this family.',
209
209
  'testFonts.families': 'Families',
210
210
  'testFonts.selectAll': 'Select all',
211
+ // Free download form
212
+ 'freeDownload.download': 'Download',
213
+ 'freeDownload.unavailable': 'These fonts aren’t available as a free download.',
214
+ 'freeDownload.freeStyles': 'Free styles: {styles}',
215
+ 'freeDownload.agreement': {
216
+ one: 'I agree to the {licenses} license',
217
+ other: 'I agree to the {licenses} licenses'
218
+ },
219
+ 'freeDownload.and': 'and',
220
+ 'freeDownload.newsletter': 'Sign me up for the newsletter',
221
+ 'freeDownload.newsletterRequired': '(required to download)',
222
+ 'freeDownload.sent': 'Check your email – we’ve sent the download link to {email}.',
211
223
  // Password-protected collections
212
224
  'password.incorrect': 'Incorrect password',
213
225
  'password.label': 'Password *',
package/dist/version.js CHANGED
@@ -8,4 +8,4 @@
8
8
  // (Fontage.ClientVersions) to spot sites that need to upgrade before a
9
9
  // feature that depends on a newer client can work for them.
10
10
  export const CLIENT_VERSION_HEADER = 'fontdue-client-version';
11
- export const version = "3.15.0";
11
+ export const version = "3.16.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fontdue-js",
3
- "version": "3.15.0",
3
+ "version": "3.16.0",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "build": "npm run relay && run-p build-js build-css build-ts",
@@ -112,6 +112,10 @@
112
112
  "react-server": "./dist/components/FontdueProvider/index.server.js",
113
113
  "default": "./dist/components/FontdueProvider/index.js"
114
114
  },
115
+ "./FreeDownloadForm": {
116
+ "react-server": "./dist/components/FreeDownloadForm/index.server.js",
117
+ "default": "./dist/components/FreeDownloadForm/index.js"
118
+ },
115
119
  "./NewsletterSignup": {
116
120
  "react-server": "./dist/components/NewsletterSignup/index.server.js",
117
121
  "default": "./dist/components/NewsletterSignup/index.js"
@@ -132,6 +136,7 @@
132
136
  "default": "./dist/components/TypeTesters/index.js"
133
137
  },
134
138
  "./useAutofit": "./dist/hooks/useAutofit.js",
139
+ "./track": "./dist/components/track.js",
135
140
  "./useConsent": "./dist/components/useConsent.js",
136
141
  "./useFont": "./dist/components/useFont.js",
137
142
  "./useFontStyle": "./dist/components/useFont.js"