@teleporthq/teleport-project-generator-next 0.45.19 → 0.45.21

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 (41) hide show
  1. package/__tests__/_helpers/data-exposure-fixture.ts +114 -44
  2. package/__tests__/component-bound-state.test.ts +817 -0
  3. package/__tests__/nav-active-links-locale.test.ts +279 -0
  4. package/__tests__/page-transition-end2end.test.ts +3 -3
  5. package/__tests__/page-transition-language-switch.test.ts +273 -0
  6. package/__tests__/security-runtime-require-paths.test.ts +29 -1
  7. package/dist/cjs/index.d.ts.map +1 -1
  8. package/dist/cjs/index.js +2 -0
  9. package/dist/cjs/index.js.map +1 -1
  10. package/dist/cjs/nav-active-link/nav-active-link-component.d.ts +1 -1
  11. package/dist/cjs/nav-active-link/nav-active-link-component.d.ts.map +1 -1
  12. package/dist/cjs/nav-active-link/nav-active-link-component.js +12 -4
  13. package/dist/cjs/nav-active-link/nav-active-link-component.js.map +1 -1
  14. package/dist/cjs/page-transition/page-transition-component.d.ts.map +1 -1
  15. package/dist/cjs/page-transition/page-transition-component.js +1 -1
  16. package/dist/cjs/page-transition/page-transition-component.js.map +1 -1
  17. package/dist/cjs/state-data-source-plugin.d.ts +27 -0
  18. package/dist/cjs/state-data-source-plugin.d.ts.map +1 -1
  19. package/dist/cjs/state-data-source-plugin.js +278 -4
  20. package/dist/cjs/state-data-source-plugin.js.map +1 -1
  21. package/dist/cjs/tsconfig.tsbuildinfo +1 -1
  22. package/dist/esm/index.d.ts.map +1 -1
  23. package/dist/esm/index.js +3 -1
  24. package/dist/esm/index.js.map +1 -1
  25. package/dist/esm/nav-active-link/nav-active-link-component.d.ts +1 -1
  26. package/dist/esm/nav-active-link/nav-active-link-component.d.ts.map +1 -1
  27. package/dist/esm/nav-active-link/nav-active-link-component.js +12 -4
  28. package/dist/esm/nav-active-link/nav-active-link-component.js.map +1 -1
  29. package/dist/esm/page-transition/page-transition-component.d.ts.map +1 -1
  30. package/dist/esm/page-transition/page-transition-component.js +1 -1
  31. package/dist/esm/page-transition/page-transition-component.js.map +1 -1
  32. package/dist/esm/state-data-source-plugin.d.ts +27 -0
  33. package/dist/esm/state-data-source-plugin.d.ts.map +1 -1
  34. package/dist/esm/state-data-source-plugin.js +278 -5
  35. package/dist/esm/state-data-source-plugin.js.map +1 -1
  36. package/dist/esm/tsconfig.tsbuildinfo +1 -1
  37. package/package.json +21 -21
  38. package/src/index.ts +6 -1
  39. package/src/nav-active-link/nav-active-link-component.ts +68 -5
  40. package/src/page-transition/page-transition-component.ts +19 -5
  41. package/src/state-data-source-plugin.ts +433 -9
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teleporthq/teleport-project-generator-next",
3
- "version": "0.45.19",
3
+ "version": "0.45.21",
4
4
  "description": "Project generator for a standard Next.js project",
5
5
  "author": "teleportHQ",
6
6
  "license": "MIT",
@@ -29,25 +29,25 @@
29
29
  "dependencies": {
30
30
  "@babel/generator": "^7.5.5",
31
31
  "@babel/types": "^7.5.5",
32
- "@teleporthq/teleport-component-generator": "^0.45.19",
33
- "@teleporthq/teleport-component-generator-react": "^0.45.19",
34
- "@teleporthq/teleport-plugin-common": "^0.45.19",
35
- "@teleporthq/teleport-plugin-css": "^0.45.19",
36
- "@teleporthq/teleport-plugin-import-statements": "^0.45.19",
37
- "@teleporthq/teleport-plugin-jsx-head-config": "^0.45.19",
38
- "@teleporthq/teleport-plugin-jsx-next-image": "^0.45.18",
39
- "@teleporthq/teleport-plugin-next-data-source": "^0.45.19",
40
- "@teleporthq/teleport-plugin-next-inline-fetch": "^0.45.19",
41
- "@teleporthq/teleport-plugin-next-static-paths": "^0.45.19",
42
- "@teleporthq/teleport-plugin-next-static-props": "^0.45.19",
43
- "@teleporthq/teleport-plugin-next-workflows": "^0.45.18",
44
- "@teleporthq/teleport-postprocessor-prettier-js": "^0.45.18",
45
- "@teleporthq/teleport-project-generator": "^0.45.19",
46
- "@teleporthq/teleport-shared": "^0.45.18",
47
- "@teleporthq/teleport-types": "^0.45.18",
48
- "@teleporthq/teleport-uidl-builders": "^0.45.18",
49
- "@teleporthq/teleport-uidl-resolver": "^0.45.19",
50
- "@teleporthq/teleport-uidl-validator": "^0.45.18"
32
+ "@teleporthq/teleport-component-generator": "^0.45.21",
33
+ "@teleporthq/teleport-component-generator-react": "^0.45.21",
34
+ "@teleporthq/teleport-plugin-common": "^0.45.21",
35
+ "@teleporthq/teleport-plugin-css": "^0.45.21",
36
+ "@teleporthq/teleport-plugin-import-statements": "^0.45.21",
37
+ "@teleporthq/teleport-plugin-jsx-head-config": "^0.45.21",
38
+ "@teleporthq/teleport-plugin-jsx-next-image": "^0.45.21",
39
+ "@teleporthq/teleport-plugin-next-data-source": "^0.45.21",
40
+ "@teleporthq/teleport-plugin-next-inline-fetch": "^0.45.21",
41
+ "@teleporthq/teleport-plugin-next-static-paths": "^0.45.21",
42
+ "@teleporthq/teleport-plugin-next-static-props": "^0.45.21",
43
+ "@teleporthq/teleport-plugin-next-workflows": "^0.45.21",
44
+ "@teleporthq/teleport-postprocessor-prettier-js": "^0.45.21",
45
+ "@teleporthq/teleport-project-generator": "^0.45.21",
46
+ "@teleporthq/teleport-shared": "^0.45.21",
47
+ "@teleporthq/teleport-types": "^0.45.21",
48
+ "@teleporthq/teleport-uidl-builders": "^0.45.21",
49
+ "@teleporthq/teleport-uidl-resolver": "^0.45.21",
50
+ "@teleporthq/teleport-uidl-validator": "^0.45.21"
51
51
  },
52
- "gitHead": "f50969b10b0368d80ec314ac66fd97b5d703c900"
52
+ "gitHead": "47f0aec1e66252803ca417857204f17dc27ed1ce"
53
53
  }
package/src/index.ts CHANGED
@@ -18,7 +18,10 @@ import {
18
18
  import { ReactStyleVariation, FileType, ProjectPlugin } from '@teleporthq/teleport-types'
19
19
  import { createStyleSheetPlugin } from '@teleporthq/teleport-plugin-css'
20
20
  import { createDocumentFileChunks, configContentGenerator } from './utils'
21
- import { createStateDataSourcePlugin } from './state-data-source-plugin'
21
+ import {
22
+ createComponentStateDataSourcePlugin,
23
+ createStateDataSourcePlugin,
24
+ } from './state-data-source-plugin'
22
25
  import { NextProjectMapping } from './next-project-mapping'
23
26
  import NextTemplate from './project-template'
24
27
  import { createNextInternationalizationPlugin } from './internationalization/locale-mapper-component'
@@ -169,6 +172,7 @@ const createNextProjectGenerator = () => {
169
172
  const nextComponentDataSourcePlugin = createNextComponentDataSourcePlugin()
170
173
  const nextPagesDataSourcePlugin = createNextPagesDataSourcePlugin()
171
174
  const stateDataSourcePlugin = createStateDataSourcePlugin()
175
+ const componentStateDataSourcePlugin = createComponentStateDataSourcePlugin()
172
176
  const nextInternationalizationPlugin = createNextInternationalizationPlugin()
173
177
  const aiChatSessionPersistencePlugin = createAIChatSessionPersistencePlugin({
174
178
  basePath: ['components'],
@@ -207,6 +211,7 @@ const createNextProjectGenerator = () => {
207
211
  nextImagePlugin,
208
212
  nextComponentInlineFetchPlugin,
209
213
  nextComponentDataSourcePlugin,
214
+ componentStateDataSourcePlugin,
210
215
  nextInternationalizationPlugin,
211
216
  // Chat-only: keeps the transcript alive across client-side
212
217
  // navigation. MUST run before the welcome plugin, so a restored
@@ -6,12 +6,20 @@
6
6
  // class and NO hardcoded `active` / `aria-current` — a hardcoded active makes
7
7
  // one link (e.g. "Taproom"/"Dashboard") look selected on EVERY page. This
8
8
  // helper adds the `active` class + `aria-current="page"` to the link whose
9
- // href matches the current page and REMOVES it from the others, on first paint
10
- // and on every client-side route change. Removing it from non-matching links
11
- // also self-corrects a link the generator may have accidentally left marked.
9
+ // href matches the current page and REMOVES it from the others, on first paint,
10
+ // on every client-side route change, and whenever links are added to the page.
11
+ // Removing it from non-matching links also self-corrects a link the generator
12
+ // may have accidentally left marked.
12
13
  //
13
14
  // It is purely additive: it never touches href/onClick, and the whole pass is
14
15
  // wrapped in try/catch, so it can never break navigation.
16
+ //
17
+ // On a multi-language site a link is current only when it leads to the page
18
+ // the visitor is on IN THE LANGUAGE they are on. `router.asPath` never carries
19
+ // the language prefix while a rendered href does (`/es/classes`), so the two
20
+ // are compared as { language, path } pairs. Comparing the bare strings marked
21
+ // no menu link on any translated page, and marked the default language's link
22
+ // in a language switcher as the current page on every one of them.
15
23
  export const NAV_ACTIVE_LINK_COMPONENT_SOURCE = `import { useEffect } from 'react'
16
24
  import { useRouter } from 'next/router'
17
25
 
@@ -28,6 +36,16 @@ function normalizePath(path) {
28
36
  return trimmed === '' ? '/' : trimmed
29
37
  }
30
38
 
39
+ // '/es/classes' -> { locale: 'es', path: '/classes' }. A path with no language
40
+ // prefix is in the default language (undefined on a single-language site).
41
+ function splitLocale(path, locales, defaultLocale) {
42
+ const segments = path.split('/')
43
+ if (locales && segments.length > 1 && locales.indexOf(segments[1]) !== -1) {
44
+ return { locale: segments[1], path: normalizePath('/' + segments.slice(2).join('/')) }
45
+ }
46
+ return { locale: defaultLocale, path: path }
47
+ }
48
+
31
49
  function collectActiveTargets(anchor) {
32
50
  const targets = [anchor]
33
51
  const descendants = anchor.querySelectorAll('*')
@@ -60,7 +78,10 @@ const NavActiveLinks = () => {
60
78
  seen.add(anchor)
61
79
  const raw = anchor.getAttribute('href') || ''
62
80
  const linkable = raw && raw.charAt(0) !== '#' && !/^(https?:|mailto:|tel:)/i.test(raw)
63
- const isActive = linkable && normalizePath(raw) === current
81
+ const target = linkable
82
+ ? splitLocale(normalizePath(raw), router.locales, router.defaultLocale)
83
+ : null
84
+ const isActive = !!target && target.path === current && target.locale === router.locale
64
85
  const targets = collectActiveTargets(anchor)
65
86
  targets.forEach((el) => {
66
87
  if (isActive) {
@@ -83,11 +104,53 @@ const NavActiveLinks = () => {
83
104
 
84
105
  apply()
85
106
  router.events.on('routeChangeComplete', apply)
107
+
108
+ // Links can be put on the page after this ran: a page that is built again
109
+ // when the visitor reaches it again (a language switch, another item of the
110
+ // same page), a menu drawn only when it opens. Whenever links are added,
111
+ // mark them, once per frame at most. Marking changes classes and attributes
112
+ // only, never the elements, so it cannot call itself.
113
+ let frame = 0
114
+ const addsLinks = (records) => {
115
+ for (let r = 0; r < records.length; r++) {
116
+ const added = records[r].addedNodes
117
+ for (let n = 0; n < added.length; n++) {
118
+ const node = added[n]
119
+ if (node.nodeType === 1 && (node.tagName === 'A' || node.querySelector('a[href]'))) {
120
+ return true
121
+ }
122
+ }
123
+ }
124
+ return false
125
+ }
126
+ const observer =
127
+ typeof MutationObserver === 'function'
128
+ ? new MutationObserver((records) => {
129
+ if (!frame && addsLinks(records)) {
130
+ frame = requestAnimationFrame(() => {
131
+ frame = 0
132
+ apply()
133
+ })
134
+ }
135
+ })
136
+ : null
137
+ if (observer) {
138
+ observer.observe(document.body, { childList: true, subtree: true })
139
+ }
140
+
86
141
  return () => {
87
142
  router.events.off('routeChangeComplete', apply)
143
+ if (observer) {
144
+ observer.disconnect()
145
+ }
146
+ if (frame) {
147
+ cancelAnimationFrame(frame)
148
+ }
88
149
  }
150
+ // A language switch keeps asPath and changes only the locale; the links it
151
+ // re-renders must be marked again once they are on the page.
89
152
  // eslint-disable-next-line react-hooks/exhaustive-deps
90
- }, [router.asPath])
153
+ }, [router.asPath, router.locale])
91
154
 
92
155
  return null
93
156
  }
@@ -187,6 +187,19 @@ const panelBounds = (panel) =>
187
187
  panel.side === 'left' ? { left: 0, right: '50%' } : panel.side === 'right' ? { left: '50%', right: 0 } : { left: 0, right: 0 }
188
188
 
189
189
  const routeKeyOf = (url) => String(url || '').split(/[?#]/)[0]
190
+ // A route event names its address with the language in front (/es/classes); a
191
+ // page's key, made from router.asPath, never carries it. Read without it, the
192
+ // two meet. Compared as they came, a language switch looked like a move to
193
+ // another page: the page was frozen to let the next one in, and no next one
194
+ // ever arrived, so it stayed frozen and could not be scrolled.
195
+ const arrivingKeyOf = (url, locales) => {
196
+ const key = routeKeyOf(url)
197
+ const segments = key.split('/')
198
+ if (locales && segments.length > 1 && locales.indexOf(segments[1]) !== -1) {
199
+ return '/' + segments.slice(2).join('/')
200
+ }
201
+ return key
202
+ }
190
203
 
191
204
  // The page changes played as a View Transition (a reveal; a flying picture), as
192
205
  // the static export plays them.
@@ -522,7 +535,8 @@ const TqPageTransition = ({ children }) => {
522
535
  return true
523
536
  }
524
537
  const onRouteChangeStart = (url) => {
525
- if (routeKeyOf(url) === routeKeyRef.current) {
538
+ const arrivingKey = arrivingKeyOf(url, router.locales)
539
+ if (arrivingKey === routeKeyRef.current) {
526
540
  return
527
541
  }
528
542
  const reverse = popRef.current
@@ -531,7 +545,7 @@ const TqPageTransition = ({ children }) => {
531
545
  // A visitor who asks for less motion gets every page that way. It is read
532
546
  // here, never while rendering: the server cannot know it, and a first render
533
547
  // that differs from the server's makes React throw the page away.
534
- const skip = prefersLessMotion() || isSkippedRoute(routeKeyOf(url))
548
+ const skip = prefersLessMotion() || isSkippedRoute(arrivingKey)
535
549
  const pointer = reverse || !ORIGIN_FROM_POINTER ? null : pointerRef.current
536
550
  // A reveal is measured on this screen (revealOnScreen); without a press it
537
551
  // grows from the screen's centre, not the page's.
@@ -541,10 +555,10 @@ const TqPageTransition = ({ children }) => {
541
555
  // Back to a page that has not gone yet (the two pages overlapped): it is
542
556
  // still in the document, pictures and all, so it plays at once and takes
543
557
  // input again.
544
- const returning = pageIn(routeKeyOf(url))
558
+ const returning = pageIn(arrivingKey)
545
559
  if (returning) {
546
560
  setLeaving(returning, false)
547
- readyKeyRef.current = routeKeyOf(url)
561
+ readyKeyRef.current = arrivingKey
548
562
  setReadyKey(readyKeyRef.current)
549
563
  }
550
564
  if (!skip && startViewChange(url, pointer, reverse)) {
@@ -559,7 +573,7 @@ const TqPageTransition = ({ children }) => {
559
573
  if (OVERLAP && !skip) {
560
574
  // The two pages overlap: the leaving page is held under the arriving
561
575
  // one until that has fully come in (HoldPage).
562
- const overlap = { leavingKey: routeKeyRef.current, arrivingKey: routeKeyOf(url) }
576
+ const overlap = { leavingKey: routeKeyRef.current, arrivingKey }
563
577
  overlap.opened = new Promise((resolve) => {
564
578
  overlap.open = resolve
565
579
  })