{"id":"haedrix/react-roblox","name":"react-roblox","scope":"haedrix","platform":"roblox","description":"The Roblox-instance renderer for React Luau.","version":"17.3.10","latest":"17.3.10","versions":["17.3.7-pre.1","17.3.7-pre.2","17.3.7","17.3.10-rc.2","17.3.10-rc.3","17.3.10-rc.4","17.3.10","17.3.10-rc.1"],"license":"MIT","licenseRating":"safe","licenseCaveats":["The package archive does not include its license text; the license is declared in its manifest metadata."],"licenseVerified":false,"dependencies":{"jsdotlua/luau-polyfill":{"version":"^1.2.7","alias":"LuauPolyfill"},"haedrix/react":{"version":"^17.3.10","alias":"React"},"haedrix/react-globals":{"version":"^17.3.10","alias":"ReactGlobals"},"haedrix/react-reconciler":{"version":"^17.3.10","alias":"ReactReconciler"},"haedrix/scheduler":{"version":"^17.3.10","alias":"Scheduler"},"haedrix/shared":{"version":"^17.3.10","alias":"Shared"}},"integrity":"5a14091df11112783cc29fcac99c7f72a45637f7fd9b303e549b8fb5b16d026e","likes":0,"downloads":0,"install":"forest install haedrix/react-roblox","url":"https://forest.dev/p/roblox/haedrix/react-roblox","files":"https://api.forest.dev/ai/package/roblox/haedrix/react-roblox/files","readme":"# react-roblox-reconciler\r\nA Roblox-opinionated renderer, intended to stand in for native renderers like `react-dom`.\r\n\r\nStatus: 🔨 Under Construction\r\n\r\n---\r\n\r\n### ✏️ Notes\r\n\r\n* Mimics pieces of the interface exported from `react-dom`\r\n\r\n# Translation Plans\r\n\r\n## Keys as Names\r\nRoact will assign the keys applied to host elements to their `Name` field to make the resulting Roblox DOM more readable and easily configurable. We need to support this behavior as well.\r\n\r\n## Migrating Bindings\r\nBindings are a Roact feature that are tightly coupled with refs, and currently implemented exclusively in `ReactRoblox` despite having some generic logic.\r\n\r\n### Relation to Refs\r\nCurrently, bindings are exposed as part of ReactRoblox:\r\n```lua\r\nReactRoblox.createBinding(nil)\r\nReactRoblox.joinBindings(binding1, binding2)\r\n```\r\n\r\nBindings are described in detail [in the Roact docs](https://roblox.github.io/roact/advanced/bindings-and-refs/#bindings). Any time a host property is assigned a binding value, Roact does the following:\r\n1. Assign the current value of the binding\r\n2. Create an updater function that assigns new values to the host property\r\n3. Subscribe to the binding object with the updater function\r\n\r\nAnd when either the component is unmounted, or the prop is assigned a different value:\r\n1. Disconnect the binding subscription\r\n2. If the component is not unmounting, assign the host prop to the new primitive value\r\n\r\n### Refs as Bindings\r\nThis will work just fine in many cases! However, in Roact, the binding implementation is used to power Refs as well. The Roblox API exposes certain host properties that must be assigned _Instance references_ as values. Effectively, there are native APIs that expect a `ref.current` value as a value.\r\n\r\nThe logic of bindings is a perfect fit for this scenario. Consider the following example:\r\n```lua\r\nlocal PopupButtons = Roact.Component:extend(\"PopupButtons\")\r\n\r\nfunction PopupButtons:init()\r\n\tself.confirmRef = Roact.createRef()\r\n\tself.cancelRef = Roact.createRef()\r\nend\r\n\r\nfunction PopupButtons:render()\r\n\t--[[\r\n\t\t\t\"Some Description\"\r\n\t\t\r\n\t\t[ Confirm ]    [ Cancel ]\r\n\t]]\r\n\treturn Roact.createElement(\"Frame\", nil {\r\n\t\tConfirmButton = Roact.createElement(\"TextButton\", {\r\n\t\t\t[Roact.Ref] = self.confirmRef,\r\n\t\t\tText = \"Confirm\",\r\n\t\t\tNextSelectionRight = self.cancelRef.value,\r\n\t\t}),\r\n\t\tCancelButton = Roact.createElement(\"TextButton\", {\r\n\t\t\t[Roact.Ref] = self.cancelRef,\r\n\t\t\tText = \"Confirm\",\r\n\t\t\tNextSelectionLeft = self.confirmRef.value,\r\n\t\t}),\r\n\t})\r\nend\r\n```\r\nThis example poses a problem. Since children will be rendered in an arbitrary order, one of the following will happen:\r\n1. Confirm Button renders first and its ref is assigned\r\n2. Confirm Button's NextSelectionRight property is set to the Cancel Button's ref, **which is currently nil**\r\n3. Cancel Button renders and its ref is assigned\r\n4. Cancel Button's NextSelectionLeft property is properly set to the Confirm Button's ref\r\n\r\nOr:\r\n1. Cancel Button renders first and its ref is assigned\r\n2. Cancel Button's NextSelectionLeft property is set to the Confirm Button's ref, **which is currently nil**\r\n3. Confirm Button renders and its ref is assigned\r\n4. Confirm Button's NextSelectionRight property is properly set to the Cancel Button's ref\r\n\r\nThus, it would require much more trickery to make even a simple gamepad neighbor assignment work correctly. However *when refs are implemented as bindings under the hood*, the above scenario can be solved pretty simply:\r\n```lua\r\n-- ...\r\n\treturn Roact.createElement(\"Frame\", nil {\r\n\t\tConfirmButton = Roact.createElement(\"TextButton\", {\r\n\t\t\t[Roact.Ref] = self.confirmRef,\r\n\t\t\tText = \"Confirm\",\r\n\t\t\t-- pass the ref itself, which is a binding\r\n\t\t\tNextSelectionRight = self.cancelRef,\r\n\t\t}),\r\n\t\tCancelButton = Roact.createElement(\"TextButton\", {\r\n\t\t\t[Roact.Ref] = self.cancelRef,\r\n\t\t\tText = \"Confirm\",\r\n\t\t\t-- pass the ref itself, which is a binding\r\n\t\t\tNextSelectionLeft = self.confirmRef,\r\n\t\t}),\r\n\t})\r\n-- ...\r\n```\r\nWith refs using binding logic, and with the above implementation, something like the following happens\r\n1. Confirm Button renders first and its ref is assigned\r\n2. Confirm Button's NextSelectionRight property is set to the Cancel Button's ref, **which is currently nil**\r\n3. Cancel Button renders and its ref is assigned\r\n\t* The binding value updates, and the Confirm button's NextSelectionRight property is assigned to the Cancel Button's new ref value\r\n4. Cancel Button's NextSelectionLeft property is properly set to the Confirm Button's ref\r\n\r\n...or the inverse, with the Cancel Button rendering first. Either way, both refs are assigned, and both neighbor properties are assigned by the time the render is complete.\r\n\r\n### Relation to Reconciler Internals\r\nBindings operate in old Roact's model, which means that they do not interact with any work queues and always update synchronously.\r\n\r\nWe should be able to carry over bindings as they are into the reconciler by simply replacing the implementation of `createRef` with a binding creation instead. This would restore the behavior described above.\r\n\r\nHowever, the abstraction of bindings leaks into the renderer when the renderer host config is responsible for understanding what bindings are and managing their subscriptions. Should we consider inverting the logic somehow?\r\n* This would add more deviations to the reconciler logic\r\n* It would be difficult (or at least delicate) to integrate this into existing reconciler logic\r\n\r\n### Proposed Strategy\r\n1. Replace `ReactCreateRef`'s implementation of `createRef` with that of Roact's\r\n\t* This will include changing the type definition of RefObject\r\n\t* We may want to also use React's typing logic rather than the Roact logic\r\n\t* We should also consider how heavily (and how) we want to obscure internals; the current bindings implementation is pretty zealous about hiding internals\r\n2. Introduce ReactRoblox tests to ensure refs as bindings behave as before\r\n3. Investigate using reconciler internals to manage binding updates. It may be reasonable to adjust how binding subscriptions work so that we can batch, defer, etc. the updates. There's a lot to consider here, so we'll need to proceed thoughtfully. We might consider doing this later on as a separate step.\r\n4. Create a `useBinding` hook. This can have essentially the same signature as `useState`, but with different semantics. By virtue of being a hook, this will make bindings usable in function components.\r\n5. Document bindings and refs. Documentation on bindings ought to make reference to `useState` as well as `useRef`.\r\n\r\n### Concerns\r\n* Upstream documentation treats refs as more general objects: https://reactjs.org/docs/hooks-faq.html#is-there-something-like-instance-variables\r\n\t* Some of the uses outlined here would might behave differently when refs are implemented as bindings\r\n\t* To conform, we'd need to make assigning to `current` equivalent to updating a binding\r\n\t* There's a part of these docs I don't understand, where it says: \"If we just wanted to set an interval, we wouldn’t need the ref (id could be local to the effect), but it’s useful if we want to clear the interval from an event handler\". It's unclear to me why you couldn't just close over a local variable the same way we're closing over intervalRef?\r\n* Is it reasonable to keep Roact's restrictions on refs? This would entail:\r\n\t* (deviation) disallow direct assignment to ref.current\r\n\t\t* When attempting to assign to ref.current, provide an error message that guides users to bindings documentation; suggest using either state or bindings, depending on intent\r\n\t* (deviation) remove the initialValue from useRef, which makes much less sense than it does in upstream where refs are generalized\r\n\t\t* If an initial value is provided, warn and suggest use of bindings or state\r\n\r\n## Text Instances\r\nWe've danced around this before; should we support text instances?\r\n* Is it remotely useful to do so?\r\n* Would it be valuable for sheer alignment purposes?\r\n\r\n## ReactDOMLegacy.findDOMNode\r\nIt's unclear whether we should port this functionality in the long run. For now, we'll hold off. Below are some tradeoffs:\r\n\r\nPros\r\n* Based on reconciler internals (`ReactFiberReconciler.findHostInstance`), which is generic reconciler logic\r\n* Should be relatively easy to port, since the reconciler implementation already exists\r\n* Would it be helpful for gamepad logic? Maybe a viable way of handling default selection?\r\n\r\nCons\r\n* It seems highly abusable, and rarely idiomatic\r\n* Not clear if there are any theoretically valid use cases besides gamepad support\r\n\r\n## ReactDOMLegacy.render and ReactDOMLegacy.unmountComponentAtNode\r\nPart of the legacy interface. This is the most recognizable react entry-point:\r\n```javascript\r\nReactDOM.render(\r\n  <h1>Hello, world!</h1>,\r\n  document.getElementById('root')\r\n);\r\n```\r\n\r\nHowever, it's also being phased out in favor of the \"root\" apis: `createLegacyRoot`, `createBlockingRoot`, and `createRoot`, the first of which should be equivalent to `render`.\r\n\r\n### Proposed Strategy\r\nThe `render` and `unmountComponentAtNode` APIs will be [phased out in React 18](https://github.com/reactwg/react-18/discussions/5), so we should strongly consider not porting them at all.\r\n\r\n## ReactDOMLegacy unstable update scheduling logic\r\nThe following scheduling-related functions are exported from ReactRoblox:\r\n* `unstable_batchedUpdates`\r\n* `unstable_flushControlled`\r\n* `unstable_runWithPriority`\r\n\r\nThese are intended to be fully released in the future. Should we exclude them until they're stabilized, or include them as is for now?\r\n\r\n## Hydration\r\n\r\n### ReactDOMLegacy.hydrate\r\nLegacy hydration; we should probably try to avoid supporting the legacy approach; it will be phased out with the roots API.\r\n\r\n### `hydrate` Option for Root\r\nRelies upon hydration support in the reconciler. Can be tackled as a single holistic feature implementation. We might want to introduce intentional, user-facing \"unimplemented\" errors that are thrown when the option is first encountered in `createRoot` implementations.\r\n\r\n### scheduleHydration\r\nReact 17 currently exports the `unstable_scheduleHydration` API. This will need investigation and implementation as part of implementing hydration\r\n\r\n## createEventHandle\r\nReact 17 exposes an `unstable_createEventHandle` API for managing event listeners on objects. Roact has its own approach to this sort of behavior via `Roact.Change` and `Roact.Event`, which are pulled into ReactRoblox.\r\n\r\nI suspect we'll simply avoid translating this logic, and call it out in documentation for `Change` and `Event`. We may also want to add a warning when accessing it that directs users to `Change` and `Event` documentation, but it may be sufficient to leave it out since it's still `unstable` anyway.\r\n\r\n## isNewReconciler\r\nReact 17 exposes an `unstable_isNewReconciler` API. This is probably unhelpful for us, since we haven't ported (and won't port) the _old_ reconciler anyways. We should probably just omit this.\r\n\r\n## renderSubtreeIntoContainer\r\nReact 17 exposes an `unstable_renderSubtreeIntoContainer` API. I'm unclear on what this does, other than it being part of the legacy rendering logic. Warnings around it say that it's deprecated, so we can likely exclude it.\r\n\r\n## input, select, option, and textarea tags\r\nThese tags have special handling in React to allow for them to play nicely with the DOM, regardless of which is used as a source of truth. Each of them is a DOM element that has its own externally-mutable state.\r\n\r\nIn order to make them behave correctly, React does a few things:\r\n* Tracks whether or not the `value` has been provided as a prop\r\n\t* If so, then changes to the value will _not_ trigger prop updates to the element\r\n\t* However, if values are not provided, the element will be changed when external changes to its underlying object are changed\r\n\t* In this way, if a component's `value` field is not set, but something else changes it externally, React will understand when re-rendering that it should not unset the value\r\n* Tracks whether or not the value has a default\r\n* Uses special logic when applying prop updates to respect whether or not the component is \"controlled\" (`value` provided as a prop, React source of truth) or \"uncontrolled\" (no `value` provided via props, DOM source of truth)\r\n\r\nIn Roact, we have similar kinds of issues with certain constructs. The only meaningful equivalent I'm aware of is `TextBox`, but it has very similar issues to those of `textarea` in ReactDOM.\r\n\r\nEquivalent behavior for `TextBox` would be nice to have, but will need careful implementation.\r\n\r\n## Roact.Change and Roact.Event\r\nThe Change and Event logic from Roact has been lifted into ReactRoblox. This logic should still behave exactly as it did in Roact. There are a couple things to address:\r\n* This logic needs to be documented as part of Roact 17's deviation documents\r\n* Exposing these as `ReactRoblox.Change` and `ReactRoblox.Event` means that component definitions need to depend upon the ReactRoblox library to provide them\r\n\t* This leaks the renderer abstraction into component definitions, which could otherwise be renderer-agnostic\r\n\t* While we don't currently have non-roblox targets, is there a possibility that this abstraction leak causes issues?\r\n\t* All of the above is also true \r\n\t* A less important concern is the slight ergonomics hit that comes from needing another import\r\n\t* Could we possibly genericize this concept and move it into React?\r\n","readmeTruncated":false}