-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathRoactTree.lua
More file actions
145 lines (132 loc) · 4.08 KB
/
RoactTree.lua
File metadata and controls
145 lines (132 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
--!strict
--[[
* Copyright (c) Roblox Corporation. All rights reserved.
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/MIT
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
]]
local Packages = script.Parent.Parent
local ReactGlobals = require(Packages.ReactGlobals)
local LuauPolyfill = require(Packages.LuauPolyfill)
local inspect = LuauPolyfill.util.inspect
local ReactRoblox = require(Packages.ReactRoblox)
type RootType = ReactRoblox.RootType
local warnOnce = require(script.Parent.warnOnce)
type RoactHandle = {
root: RootType,
key: string | number,
parent: any, -- ROBLOX TODO: Instance?
}
@[deprecated{ use = "ReactRoblox.createRoot()"}]
local function mount(element: any, parent: any, key: string?): RoactHandle
if ReactGlobals.__DEV__ and ReactGlobals.__COMPAT_WARNINGS__ then
warnOnce("mount", "Please use the createRoot API in ReactRoblox")
end
if parent ~= nil and typeof(parent) ~= "Instance" then
error(
string.format(
"Cannot mount element (`%s`) into a parent that is not a Roblox Instance (got type `%s`) \n%s",
(function()
if element then
return tostring(element.type)
end
return "<unknown>"
end)(),
typeof(parent),
(function()
if parent ~= nil then
return inspect(parent)
end
return ""
end)()
)
)
end
-- Since we use portals to actually parent to the provided parent argument,
-- the container instance that we provide to createRoot is just a
-- dummy instance.
local root
if _G.__ROACT_17_COMPAT_LEGACY_ROOT__ then
root = ReactRoblox.createLegacyRoot(Instance.new("Folder"))
else
root = ReactRoblox.createRoot(Instance.new("Folder"))
end
if parent == nil then
local newParent = Instance.new("Folder")
newParent.Name = "Target"
parent = newParent
end
if key == nil then
if _G.__ROACT_17_COMPAT_LEGACY_ROOT__ then
key = "ReactLegacyRoot"
else
key = "ReactRoot"
end
end
-- ROBLOX TODO: remove INLINE_ACT flag when all tests are updated to use
-- `act` explicitly
if ReactGlobals.__ROACT_17_INLINE_ACT__ then
ReactRoblox.act(function()
root:render(ReactRoblox.createPortal({ [key] = element }, parent))
end)
else
root:render(ReactRoblox.createPortal({ [key] = element }, parent))
end
return {
root = root,
-- To preserve the same key and portal to the same parent on update, we
-- need to stash them in the opaque "tree" reference returned by `mount`
parent = parent,
key = key :: string,
}
end
@[deprecated{ use = "ReactRoblox.createRoot()"}]
local function update(roactHandle: RoactHandle, element)
if ReactGlobals.__DEV__ and ReactGlobals.__COMPAT_WARNINGS__ then
warnOnce("update", "Please use the createRoot API in ReactRoblox")
end
local key = roactHandle.key
local parent = roactHandle.parent
-- ROBLOX TODO: remove INLINE_ACT flag when all tests are updated to use
-- `act` explicitly
if ReactGlobals.__ROACT_17_INLINE_ACT__ then
ReactRoblox.act(function()
roactHandle.root:render(
ReactRoblox.createPortal({ [key :: string] = element }, parent)
)
end)
else
roactHandle.root:render(
ReactRoblox.createPortal({ [key :: string] = element }, parent)
)
end
return roactHandle
end
@[deprecated{ use = "ReactRoblox.createRoot()"}]
local function unmount(roactHandle: RoactHandle)
if ReactGlobals.__DEV__ and ReactGlobals.__COMPAT_WARNINGS__ then
warnOnce("unmount", "Please use the createRoot API in ReactRoblox")
end
-- ROBLOX TODO: remove INLINE_ACT flag when all tests are updated to use
-- `act` explicitly
if ReactGlobals.__ROACT_17_INLINE_ACT__ then
ReactRoblox.act(function()
roactHandle.root:unmount()
end)
else
roactHandle.root:unmount()
end
end
return {
mount = mount,
update = update,
unmount = unmount,
}