@cratis/scene.react 4.0.0 → 4.1.1

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 (79) hide show
  1. package/dist/cjs/SceneElementView.js +1 -0
  2. package/dist/cjs/SceneElementView.js.map +1 -1
  3. package/dist/cjs/index.js +8 -0
  4. package/dist/cjs/index.js.map +1 -1
  5. package/dist/cjs/interactions/ConfirmHost.js +71 -0
  6. package/dist/cjs/interactions/ConfirmHost.js.map +1 -0
  7. package/dist/cjs/interactions/InteractionScope.js +10 -1
  8. package/dist/cjs/interactions/InteractionScope.js.map +1 -1
  9. package/dist/cjs/interactions/NotificationHost.js +73 -0
  10. package/dist/cjs/interactions/NotificationHost.js.map +1 -0
  11. package/dist/cjs/interactions/UnresolvedActionHost.js +53 -0
  12. package/dist/cjs/interactions/UnresolvedActionHost.js.map +1 -0
  13. package/dist/cjs/interactions/useInteractionHosts.js +109 -0
  14. package/dist/cjs/interactions/useInteractionHosts.js.map +1 -0
  15. package/dist/cjs/renderer/InteractiveComponent.js +1 -0
  16. package/dist/cjs/renderer/InteractiveComponent.js.map +1 -1
  17. package/dist/esm/SceneElementView.js +1 -0
  18. package/dist/esm/SceneElementView.js.map +1 -1
  19. package/dist/esm/for_useInteractionHosts/when_a_document_asks_the_user_something.d.ts +2 -0
  20. package/dist/esm/for_useInteractionHosts/when_a_document_asks_the_user_something.d.ts.map +1 -0
  21. package/dist/esm/for_useInteractionHosts/when_a_document_asks_the_user_something.js +73 -0
  22. package/dist/esm/for_useInteractionHosts/when_a_document_asks_the_user_something.js.map +1 -0
  23. package/dist/esm/for_useInteractionHosts/when_the_host_cannot_do_what_was_asked.d.ts +2 -0
  24. package/dist/esm/for_useInteractionHosts/when_the_host_cannot_do_what_was_asked.d.ts.map +1 -0
  25. package/dist/esm/for_useInteractionHosts/when_the_host_cannot_do_what_was_asked.js +43 -0
  26. package/dist/esm/for_useInteractionHosts/when_the_host_cannot_do_what_was_asked.js.map +1 -0
  27. package/dist/esm/index.js +4 -0
  28. package/dist/esm/index.js.map +1 -1
  29. package/dist/esm/interactions/ConfirmHost.d.ts +8 -0
  30. package/dist/esm/interactions/ConfirmHost.d.ts.map +1 -0
  31. package/dist/esm/interactions/ConfirmHost.js +69 -0
  32. package/dist/esm/interactions/ConfirmHost.js.map +1 -0
  33. package/dist/esm/interactions/ConfirmRequest.d.ts +5 -0
  34. package/dist/esm/interactions/ConfirmRequest.d.ts.map +1 -0
  35. package/dist/esm/interactions/ConfirmRequest.js +2 -0
  36. package/dist/esm/interactions/ConfirmRequest.js.map +1 -0
  37. package/dist/esm/interactions/InteractionScope.d.ts +2 -1
  38. package/dist/esm/interactions/InteractionScope.d.ts.map +1 -1
  39. package/dist/esm/interactions/InteractionScope.js +10 -1
  40. package/dist/esm/interactions/InteractionScope.js.map +1 -1
  41. package/dist/esm/interactions/NotificationHost.d.ts +7 -0
  42. package/dist/esm/interactions/NotificationHost.d.ts.map +1 -0
  43. package/dist/esm/interactions/NotificationHost.js +71 -0
  44. package/dist/esm/interactions/NotificationHost.js.map +1 -0
  45. package/dist/esm/interactions/SceneNotification.d.ts +7 -0
  46. package/dist/esm/interactions/SceneNotification.d.ts.map +1 -0
  47. package/dist/esm/interactions/SceneNotification.js +2 -0
  48. package/dist/esm/interactions/SceneNotification.js.map +1 -0
  49. package/dist/esm/interactions/UnresolvedAction.d.ts +5 -0
  50. package/dist/esm/interactions/UnresolvedAction.d.ts.map +1 -0
  51. package/dist/esm/interactions/UnresolvedAction.js +2 -0
  52. package/dist/esm/interactions/UnresolvedAction.js.map +1 -0
  53. package/dist/esm/interactions/UnresolvedActionHost.d.ts +6 -0
  54. package/dist/esm/interactions/UnresolvedActionHost.d.ts.map +1 -0
  55. package/dist/esm/interactions/UnresolvedActionHost.js +51 -0
  56. package/dist/esm/interactions/UnresolvedActionHost.js.map +1 -0
  57. package/dist/esm/interactions/index.d.ts +7 -0
  58. package/dist/esm/interactions/index.d.ts.map +1 -1
  59. package/dist/esm/interactions/index.js +7 -0
  60. package/dist/esm/interactions/index.js.map +1 -1
  61. package/dist/esm/interactions/useInteractionHosts.d.ts +11 -0
  62. package/dist/esm/interactions/useInteractionHosts.d.ts.map +1 -0
  63. package/dist/esm/interactions/useInteractionHosts.js +107 -0
  64. package/dist/esm/interactions/useInteractionHosts.js.map +1 -0
  65. package/dist/esm/renderer/InteractiveComponent.js +1 -0
  66. package/dist/esm/renderer/InteractiveComponent.js.map +1 -1
  67. package/dist/esm/tsconfig.tsbuildinfo +1 -1
  68. package/for_useInteractionHosts/when_a_document_asks_the_user_something.tsx +106 -0
  69. package/for_useInteractionHosts/when_the_host_cannot_do_what_was_asked.tsx +66 -0
  70. package/interactions/ConfirmHost.tsx +42 -0
  71. package/interactions/ConfirmRequest.ts +19 -0
  72. package/interactions/InteractionScope.tsx +22 -2
  73. package/interactions/NotificationHost.tsx +48 -0
  74. package/interactions/SceneNotification.ts +18 -0
  75. package/interactions/UnresolvedAction.ts +17 -0
  76. package/interactions/UnresolvedActionHost.tsx +33 -0
  77. package/interactions/index.ts +7 -0
  78. package/interactions/useInteractionHosts.tsx +101 -0
  79. package/package.json +5 -5
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useInteractionHosts.js","sources":["../../../interactions/useInteractionHosts.tsx"],"sourcesContent":["// Copyright (c) Cratis. All rights reserved.\n// Licensed under the MIT license. See LICENSE file in the project root for full license information.\n\nimport { ReactNode, useCallback, useMemo, useRef, useState } from 'react';\nimport { NotificationLevel } from '@cratis/scene.model';\nimport { BrowserDispatcherHost } from './createBrowserDispatcher';\nimport { ConfirmHost } from './ConfirmHost';\nimport { ConfirmRequest } from './ConfirmRequest';\nimport { NotificationHost } from './NotificationHost';\nimport { SceneNotification } from './SceneNotification';\nimport { UnresolvedAction } from './UnresolvedAction';\nimport { UnresolvedActionHost } from './UnresolvedActionHost';\n\n/**\n * What {@link useInteractionHosts} gives an application.\n */\nexport interface InteractionHosts {\n /** The parts of a {@link BrowserDispatcherHost} these surfaces provide. Spread it into the host you build. */\n readonly host: Pick<BrowserDispatcherHost, 'notify' | 'confirm' | 'onUnsupported'>;\n\n /** The surfaces themselves. Render them once, anywhere inside the application. */\n readonly surfaces: ReactNode;\n}\n\n/**\n * How long a notification stays before it removes itself, in milliseconds.\n */\nexport interface InteractionHostOptions {\n /** How long a notification stays up. `0` keeps it until it is dismissed. */\n readonly dismissAfter?: number;\n}\n\n/**\n * Provides the default surfaces for the actions that need somewhere to appear.\n *\n * `notify` and `confirm` are the two actions a document can take that have no backend and no route: they are\n * purely things the user sees. Without a surface, a host either invents one or the action goes to the console,\n * which is why these ship with Scene rather than being every application's first chore.\n *\n * The three are returned as one unit because they are wired the same way and forgetting to render one of them\n * is exactly the failure they exist to prevent:\n *\n * ```tsx\n * const { host, surfaces } = useInteractionHosts();\n * const dispatcher = useMemo(() => createBrowserDispatcher({ ...host, executeCommand, navigate }), [host]);\n *\n * return <InteractionScope dispatcher={dispatcher}>{children}{surfaces}</InteractionScope>;\n * ```\n *\n * An application with a design system of its own passes its own `notify` after the spread and keeps the rest.\n * @param options The {@link InteractionHostOptions}.\n * @returns The {@link InteractionHosts}.\n */\nexport function useInteractionHosts(options: InteractionHostOptions = {}): InteractionHosts {\n const { dismissAfter = 6000 } = options;\n const [notifications, setNotifications] = useState<readonly SceneNotification[]>([]);\n const [unresolved, setUnresolved] = useState<readonly UnresolvedAction[]>([]);\n const [request, setRequest] = useState<ConfirmRequest | undefined>(undefined);\n const nextId = useRef(0);\n\n const dismiss = useCallback((id: number) => setNotifications(current => current.filter(_ => _.id !== id)), []);\n\n const notify = useCallback((level: NotificationLevel, message: string) => {\n const id = nextId.current++;\n setNotifications(current => [...current, { id, level, message }]);\n\n if (dismissAfter > 0) {\n setTimeout(() => setNotifications(current => current.filter(_ => _.id !== id)), dismissAfter);\n }\n }, [dismissAfter]);\n\n const confirm = useCallback((message: string) => new Promise<boolean>(resolve => {\n setRequest({\n message,\n answer(confirmed) {\n // Cleared before resolving, so the sequence that continues cannot find a question on screen\n // that has already been answered.\n setRequest(undefined);\n resolve(confirmed);\n },\n });\n }), []);\n\n const onUnsupported = useCallback((action: string) => {\n // Reported once per distinct action. An unsupported action on a hover or an interval would otherwise\n // grow the list until it covered the application it is trying to tell you about.\n setUnresolved(current => current.some(_ => _.action === action) ? current : [...current, { id: nextId.current++, action }]);\n }, []);\n\n const host = useMemo(() => ({ notify, confirm, onUnsupported }), [notify, confirm, onUnsupported]);\n\n const surfaces = useMemo(() => (\n <>\n <NotificationHost notifications={notifications} onDismiss={dismiss} />\n <ConfirmHost request={request} />\n <UnresolvedActionHost unresolved={unresolved} />\n </>\n ), [notifications, dismiss, request, unresolved]);\n\n return { host, surfaces };\n}\n"],"names":["useInteractionHosts","options","dismissAfter","notifications","setNotifications","useState","unresolved","setUnresolved","request","setRequest","undefined","nextId","useRef","dismiss","useCallback","id","current","filter","_","notify","level","message","setTimeout","confirm","Promise","resolve","answer","confirmed","onUnsupported","action","some","host","useMemo","surfaces","_jsxs","_Fragment","_jsx","NotificationHost","onDismiss","ConfirmHost","UnresolvedActionHost"],"mappings":";;;;;;AAgCA;;;;;;;;;;;;;;;;;;;;AAoBC,IACM,SAASA,mBAAAA,CAAoBC,OAAAA,GAAkC,EAAE,EAAA;AACpE,IAAA,MAAM,EAAEC,YAAAA,GAAe,IAAI,EAAE,GAAGD,OAAAA;AAChC,IAAA,MAAM,CAACE,aAAAA,EAAeC,gBAAAA,CAAiB,GAAGC,SAAuC,EAAE,CAAA;AACnF,IAAA,MAAM,CAACC,UAAAA,EAAYC,aAAAA,CAAc,GAAGF,SAAsC,EAAE,CAAA;AAC5E,IAAA,MAAM,CAACG,OAAAA,EAASC,UAAAA,CAAW,GAAGJ,QAAAA,CAAqCK,SAAAA,CAAAA;AACnE,IAAA,MAAMC,SAASC,MAAAA,CAAO,CAAA,CAAA;AAEtB,IAAA,MAAMC,UAAUC,WAAAA,CAAY,CAACC,EAAAA,GAAeX,gBAAAA,CAAiBY,CAA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KA,MAAAA,CAAAA,GAAUb,OAAAA,GAAU;AAAIA,gBAAAA,GAAAA,OAAAA;AAAS,gBAAA;AAAED,oBAAAA,EAAAA,EAAIJ,OAAOK,OAAO,EAAA;AAAIa,oBAAAA;AAAO;AAAE,aAAA,CAAA;AAC9H,IAAA,CAAA,EAAG,EAAE,CAAA;IAEL,MAAME,IAAAA,GAAOC,OAAAA,CAAQ,KAAO;AAAEb,YAAAA,MAAAA;AAAQI,YAAAA,OAAAA;AAASK,YAAAA;AAAc,SAAA,CAAA,EAAI;AAACT,QAAAA,MAAAA;AAAQI,QAAAA,OAAAA;AAASK,QAAAA;AAAc,KAAA,CAAA;IAEjG,MAAMK,QAAAA,GAAWD,QAAQ,kBACrBE,IAAA,CAAAC,QAAA,EAAA;;8BACIC,GAAA,CAACC,gBAAAA,EAAAA;oBAAiBlC,aAAAA,EAAeA,aAAAA;oBAAemC,SAAAA,EAAWzB;;8BAC3DuB,GAAA,CAACG,WAAAA,EAAAA;oBAAY/B,OAAAA,EAASA;;8BACtB4B,GAAA,CAACI,oBAAAA,EAAAA;oBAAqBlC,UAAAA,EAAYA;;;AAEvC,SAAA,CAAA,EAAA;AAACH,QAAAA,aAAAA;AAAeU,QAAAA,OAAAA;AAASL,QAAAA,OAAAA;AAASF,QAAAA;AAAW,KAAA,CAAA;IAEhD,OAAO;AAAEyB,QAAAA,IAAAA;AAAME,QAAAA;AAAS,KAAA;AAC5B;;"}
@@ -1,6 +1,7 @@
1
1
  import { createElement } from 'react';
2
2
  import 'react/jsx-runtime';
3
3
  import { useInteractions } from '../interactions/useInteractions.js';
4
+ import '../interactions/NotificationHost.js';
4
5
 
5
6
  // Copyright (c) Cratis. All rights reserved.
6
7
  // Licensed under the MIT license. See LICENSE file in the project root for full license information.
@@ -1 +1 @@
1
- {"version":3,"file":"InteractiveComponent.js","sources":["../../../renderer/InteractiveComponent.tsx"],"sourcesContent":["// Copyright (c) Cratis. All rights reserved.\n// Licensed under the MIT license. See LICENSE file in the project root for full license information.\n\nimport { ComponentType, ReactNode, createElement } from 'react';\nimport { ExternalComponent } from '@cratis/scene.model';\nimport { useInteractions } from '../interactions';\nimport { RegisteredComponentProps } from './ComponentRegistry';\n\nexport interface InteractiveComponentProps {\n component: ComponentType<RegisteredComponentProps>;\n element: ExternalComponent;\n slots: Record<string, ReactNode[]>;\n}\n\n/**\n * Resolves an element's interactions and hands them to the component that renders it.\n *\n * This exists as a component rather than as a call inside the renderer because resolving them is a hook, and a\n * hook cannot run inside the plain function a {@link Renderer} is. Keeping the renderer plain is what lets the\n * same element tree be rendered by something that is not React at all.\n */\nexport function InteractiveComponent({ component, element, slots }: InteractiveComponentProps) {\n const interactions = useInteractions(element.id, element.behaviors);\n return createElement(component, { element, slots, interactions });\n}\n"],"names":["InteractiveComponent","component","element","slots","interactions","useInteractions","id","behaviors","createElement"],"mappings":";;;;AAAA;AACA;AAaA;;;;;;IAOO,SAASA,oBAAAA,CAAqB,EAAEC,SAAS,EAAEC,OAAO,EAAEC,KAAK,EAA6B,EAAA;AACzF,IAAA,MAAMC,eAAeC,eAAAA,CAAgBH,OAAAA,CAAQI,EAAE,EAAEJ,QAAQK,SAAS,CAAA;AAClE,IAAA,qBAAOC,cAAcP,SAAAA,EAAW;AAAEC,QAAAA,OAAAA;AAASC,QAAAA,KAAAA;AAAOC,QAAAA;AAAa,KAAA,CAAA;AACnE;;"}
1
+ {"version":3,"file":"InteractiveComponent.js","sources":["../../../renderer/InteractiveComponent.tsx"],"sourcesContent":["// Copyright (c) Cratis. All rights reserved.\n// Licensed under the MIT license. See LICENSE file in the project root for full license information.\n\nimport { ComponentType, ReactNode, createElement } from 'react';\nimport { ExternalComponent } from '@cratis/scene.model';\nimport { useInteractions } from '../interactions';\nimport { RegisteredComponentProps } from './ComponentRegistry';\n\nexport interface InteractiveComponentProps {\n component: ComponentType<RegisteredComponentProps>;\n element: ExternalComponent;\n slots: Record<string, ReactNode[]>;\n}\n\n/**\n * Resolves an element's interactions and hands them to the component that renders it.\n *\n * This exists as a component rather than as a call inside the renderer because resolving them is a hook, and a\n * hook cannot run inside the plain function a {@link Renderer} is. Keeping the renderer plain is what lets the\n * same element tree be rendered by something that is not React at all.\n */\nexport function InteractiveComponent({ component, element, slots }: InteractiveComponentProps) {\n const interactions = useInteractions(element.id, element.behaviors);\n return createElement(component, { element, slots, interactions });\n}\n"],"names":["InteractiveComponent","component","element","slots","interactions","useInteractions","id","behaviors","createElement"],"mappings":";;;;;AAAA;AACA;AAaA;;;;;;IAOO,SAASA,oBAAAA,CAAqB,EAAEC,SAAS,EAAEC,OAAO,EAAEC,KAAK,EAA6B,EAAA;AACzF,IAAA,MAAMC,eAAeC,eAAAA,CAAgBH,OAAAA,CAAQI,EAAE,EAAEJ,QAAQK,SAAS,CAAA;AAClE,IAAA,qBAAOC,cAAcP,SAAAA,EAAW;AAAEC,QAAAA,OAAAA;AAASC,QAAAA,KAAAA;AAAOC,QAAAA;AAAa,KAAA,CAAA;AACnE;;"}