@nocode-factory/sanity-kit 2.33.2 → 2.34.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,6 +1,6 @@
1
1
  {
2
2
  "name": "@nocode-factory/sanity-kit",
3
- "version": "2.33.2",
3
+ "version": "2.34.0",
4
4
  "description": "Outils Studio de NoCode Factory : Pages (liste + board), Composants, Historique",
5
5
  "license": "UNLICENSED",
6
6
  "private": false,
@@ -61,7 +61,8 @@
61
61
  "peerDependencies": {
62
62
  "next": ">=15",
63
63
  "react": "^19.2",
64
- "sanity": "^6.0.0"
64
+ "sanity": "^6.0.0",
65
+ "react-dom": "^19.2"
65
66
  },
66
67
  "devDependencies": {
67
68
  "@sanity/pkg-utils": "13.0.1",
@@ -73,7 +74,8 @@
73
74
  "sanity": "6.15.0",
74
75
  "styled-components": "^6.1.18",
75
76
  "typescript": "^5.9.3",
76
- "vitest": "^4.1.11"
77
+ "vitest": "^4.1.11",
78
+ "@types/react-dom": "^19.2.3"
77
79
  },
78
80
  "publishConfig": {
79
81
  "registry": "https://registry.npmjs.org",
@@ -1,23 +1,39 @@
1
- import {useEffect, useRef, useState} from 'react'
2
- import {Button, Flex, Box} from '@sanity/ui'
1
+ import {useEffect, useLayoutEffect, useRef, useState} from 'react'
2
+ import {createPortal} from 'react-dom'
3
+ import {Box, Button, Flex} from '@sanity/ui'
4
+ import {Menu, MenuButton, MenuDivider, MenuItem} from '@sanity/ui/menu'
3
5
  import {TabletDeviceIcon} from '@sanity/icons/TabletDevice'
4
6
  import {PanelRightIcon} from '@sanity/icons/PanelRight'
5
7
  import type {PreviewHeaderProps} from 'sanity/presentation'
6
8
 
7
- const TABLET_WIDTH = 768
8
- const KEY_TABLET = 'ncf.presentation.tablet'
9
+ /** Appareils proposés (dimensions CSS de l'écran, portrait). */
10
+ export const PREVIEW_DEVICES = [
11
+ {id: 'iphone-se', label: 'iPhone SE', width: 375, height: 667},
12
+ {id: 'iphone-12-pro', label: 'iPhone 12 Pro', width: 390, height: 844},
13
+ {id: 'iphone-15-pro-max', label: 'iPhone 15 Pro Max', width: 430, height: 932},
14
+ {id: 'pixel-7', label: 'Pixel 7', width: 412, height: 915},
15
+ {id: 'galaxy-s20', label: 'Galaxy S20', width: 360, height: 800},
16
+ {id: 'ipad-mini', label: 'iPad Mini', width: 768, height: 1024},
17
+ {id: 'ipad-air', label: 'iPad Air', width: 820, height: 1180},
18
+ {id: 'ipad-pro-11', label: 'iPad Pro 11"', width: 834, height: 1194},
19
+ {id: 'ipad-pro-13', label: 'iPad Pro 12.9"', width: 1024, height: 1366},
20
+ ] as const
21
+ type DeviceId = (typeof PREVIEW_DEVICES)[number]['id']
22
+
23
+ const KEY_DEVICE = 'ncf.presentation.device'
9
24
  const KEY_HIDE = 'ncf.presentation.hideForm'
10
25
 
11
- const load = (key: string) => {
26
+ const read = (key: string): string | null => {
12
27
  try {
13
- return window.localStorage.getItem(key) === '1'
28
+ return window.localStorage.getItem(key)
14
29
  } catch {
15
- return false
30
+ return null
16
31
  }
17
32
  }
18
- const save = (key: string, on: boolean) => {
33
+ const write = (key: string, value: string | null) => {
19
34
  try {
20
- window.localStorage.setItem(key, on ? '1' : '0')
35
+ if (value === null) window.localStorage.removeItem(key)
36
+ else window.localStorage.setItem(key, value)
21
37
  } catch {
22
38
  /* stockage indisponible : le choix vaut pour la session */
23
39
  }
@@ -29,75 +45,115 @@ const previewPanel = () => document.getElementById('presentation-narrow-panel-pr
29
45
  const contentPanel = () => document.getElementById('presentation-narrow-panel-content')
30
46
 
31
47
  /**
32
- * En-tête de l'aperçu (Presentation) : la barre d'adresse native, plus
33
- * - « Tablet » : aperçu à 768 px, centré (Presentation n'a qu'ordinateur et mobile) ;
34
- * - « Hide edit panel » : masque le formulaire à droite, l'aperçu prend la place
35
- * (la liste des pages à gauche reste).
36
- * Choix mémorisés dans le navigateur. Aucune donnée touchée : seulement l'affichage.
48
+ * En-tête de l'aperçu (Presentation) : la barre native, plus
49
+ * - un menu d'APPAREILS, posé juste à côté du bouton mobile natif : iPhone 12 Pro
50
+ * (390×844), iPad Air (820×1180)... L'aperçu prend les dimensions de l'appareil,
51
+ * affichées à côté de l'icône ;
52
+ * - « Hide edit panel » : masque le formulaire à droite (la liste des pages reste).
53
+ * Choix mémorisés dans le navigateur. Seulement l'affichage : aucune donnée touchée.
37
54
  */
38
55
  export function NcfPresentationHeader(props: PreviewHeaderProps) {
39
56
  const ref = useRef<HTMLDivElement>(null)
40
- const [tablet, setTablet] = useState(() => load(KEY_TABLET))
41
- const [hideForm, setHideForm] = useState(() => load(KEY_HIDE))
57
+ const [deviceId, setDeviceId] = useState<DeviceId | null>(() => (read(KEY_DEVICE) as DeviceId | null) ?? null)
58
+ const [hideForm, setHideForm] = useState(() => read(KEY_HIDE) === '1')
59
+ const [slot, setSlot] = useState<HTMLElement | null>(null)
60
+ const device = PREVIEW_DEVICES.find((d) => d.id === deviceId) ?? null
42
61
 
43
- // Tablette : largeur maximale de l'iframe de l'aperçu, centrée.
44
- useEffect(() => {
45
- const iframe = previewPanel()?.querySelector('iframe') ?? ref.current?.closest('div[style]')?.parentElement?.querySelector('iframe') ?? null
46
- if (!iframe) return
47
- if (tablet) {
48
- iframe.style.setProperty('max-width', `${TABLET_WIDTH}px`, 'important')
49
- iframe.style.setProperty('margin-left', 'auto', 'important')
50
- iframe.style.setProperty('margin-right', 'auto', 'important')
51
- iframe.style.setProperty('display', 'block', 'important')
62
+ // Emplacement juste après le bouton mobile / ordinateur natif de Presentation.
63
+ useLayoutEffect(() => {
64
+ const root = ref.current
65
+ const icon = root?.querySelector('[data-sanity-icon="mobile-device"], [data-sanity-icon="desktop"]')
66
+ const button = icon?.closest('button') ?? null
67
+ if (!button) return
68
+ let target = root?.querySelector<HTMLElement>('[data-ncf-device-slot]') ?? null
69
+ if (!target) {
70
+ target = document.createElement('span')
71
+ target.setAttribute('data-ncf-device-slot', '')
72
+ target.style.display = 'inline-flex'
73
+ button.insertAdjacentElement('afterend', target)
52
74
  }
75
+ if (target !== slot) setSlot(target)
76
+ })
77
+
78
+ // Appareil : l'iframe de l'aperçu prend ses dimensions (dans la place disponible).
79
+ useEffect(() => {
80
+ const iframe = previewPanel()?.querySelector('iframe') ?? null
81
+ if (!iframe || !device) return
82
+ const set: [string, string][] = [
83
+ ['width', `${device.width}px`],
84
+ ['max-width', '100%'],
85
+ ['height', `${device.height}px`],
86
+ ['margin-left', 'auto'],
87
+ ['margin-right', 'auto'],
88
+ ['display', 'block'],
89
+ ['flex', 'none'],
90
+ ]
91
+ for (const [p, v] of set) iframe.style.setProperty(p, v, 'important')
53
92
  return () => {
54
- for (const p of ['max-width', 'margin-left', 'margin-right', 'display']) iframe.style.removeProperty(p)
93
+ for (const [p] of set) iframe.style.removeProperty(p)
55
94
  }
56
95
  })
57
96
 
58
- // Panneau d'édition masqué : panneaux à droite de l'aperçu cachés, aperçu élargi.
97
+ // Panneau d'édition masqué : lui et sa poignée cachés, l'aperçu prend la place.
59
98
  useEffect(() => {
60
99
  const panel = previewPanel()
61
100
  const content = contentPanel()
62
- if (!panel || !content || !hideForm) return
63
- // Le panneau d'édition et sa poignée de redimensionnement (juste avant lui).
101
+ if (!content || !hideForm) return
64
102
  const hidden: HTMLElement[] = [content]
65
103
  const handle = content.previousElementSibling as HTMLElement | null
66
104
  if (handle && handle !== panel && !handle.id) hidden.push(handle)
105
+ // Seulement masquer : jamais toucher aux largeurs gérées par Presentation.
67
106
  const apply = () => {
68
107
  for (const h of hidden) if (h.style.getPropertyValue('display') !== 'none') h.style.setProperty('display', 'none', 'important')
69
- if (panel.style.getPropertyPriority('flex-grow') !== 'important') panel.style.setProperty('flex-grow', '1000', 'important')
70
108
  }
71
109
  apply()
72
- // Presentation réécrit les largeurs de ses panneaux (redimensionnement) : on
73
- // réapplique, sans boucle (rien n'est réécrit si c'est déjà en place).
74
110
  const observer = new MutationObserver(apply)
75
- for (const el of [panel, ...hidden]) observer.observe(el, {attributes: true, attributeFilter: ['style']})
111
+ for (const el of hidden) observer.observe(el, {attributes: true, attributeFilter: ['style']})
76
112
  return () => {
77
113
  observer.disconnect()
78
114
  for (const h of hidden) h.style.removeProperty('display')
79
- panel.style.removeProperty('flex-grow')
80
115
  }
81
116
  })
82
117
 
118
+ const choose = (id: DeviceId | null) => {
119
+ setDeviceId(id)
120
+ write(KEY_DEVICE, id)
121
+ }
122
+
123
+ const deviceMenu = (
124
+ <MenuButton
125
+ id="ncf-preview-device"
126
+ button={
127
+ <Button
128
+ icon={TabletDeviceIcon}
129
+ text={device ? `${device.width}×${device.height}` : undefined}
130
+ mode={device ? 'default' : 'bleed'}
131
+ tone={device ? 'primary' : 'default'}
132
+ padding={2}
133
+ fontSize={1}
134
+ title={device ? `${device.label} (${device.width}×${device.height})` : 'Preview on a device'}
135
+ />
136
+ }
137
+ menu={
138
+ <Menu>
139
+ <MenuItem text="Full width" pressed={!device} onClick={() => choose(null)} />
140
+ <MenuDivider />
141
+ {PREVIEW_DEVICES.map((d) => (
142
+ <MenuItem key={d.id} text={`${d.label} ${d.width}×${d.height}`} pressed={d.id === deviceId} onClick={() => choose(d.id)} />
143
+ ))}
144
+ </Menu>
145
+ }
146
+ popover={{portal: true, placement: 'bottom-end'}}
147
+ />
148
+ )
149
+
83
150
  return (
84
151
  <Flex ref={ref} align="center" gap={1} style={{width: '100%'}}>
85
152
  <Box flex={1} style={{minWidth: 0}}>
86
153
  {props.renderDefault(props)}
87
154
  </Box>
88
- <Button
89
- icon={TabletDeviceIcon}
90
- mode={tablet ? 'default' : 'bleed'}
91
- tone={tablet ? 'primary' : 'default'}
92
- padding={2}
93
- fontSize={1}
94
- title={tablet ? 'Back to full width' : `Tablet (${TABLET_WIDTH}px)`}
95
- aria-pressed={tablet}
96
- onClick={() => {
97
- setTablet(!tablet)
98
- save(KEY_TABLET, !tablet)
99
- }}
100
- />
155
+ {/* À côté du bouton mobile natif ; à défaut (mise en page de Sanity changée), ici. */}
156
+ {slot ? createPortal(deviceMenu, slot) : deviceMenu}
101
157
  <Button
102
158
  icon={PanelRightIcon}
103
159
  mode={hideForm ? 'default' : 'bleed'}
@@ -108,7 +164,7 @@ export function NcfPresentationHeader(props: PreviewHeaderProps) {
108
164
  aria-pressed={hideForm}
109
165
  onClick={() => {
110
166
  setHideForm(!hideForm)
111
- save(KEY_HIDE, !hideForm)
167
+ write(KEY_HIDE, !hideForm ? '1' : '0')
112
168
  }}
113
169
  />
114
170
  </Flex>
package/src/version.ts CHANGED
@@ -3,4 +3,4 @@
3
3
  * quelle version. Recopiée de `package.json` par `npm version` (scripts/sync-version.mjs) ; un test
4
4
  * le vérifie.
5
5
  */
6
- export const KIT_VERSION = '2.33.2'
6
+ export const KIT_VERSION = '2.34.0'