Skip to content

Commit

Permalink
Fix Roact element rendering (#18)
Browse files Browse the repository at this point in the history
# Problem

Setting a story to be the result of `Roact.createElement(FooComponent)`
results in errors. This is an expect use case for Roact stories that
don't accept controls and should be supported

# Solution

Added support for pre-made Roact elements and also took a pass to make
sure React and Roact primitives could be rendered as well
  • Loading branch information
vocksel authored Nov 3, 2024
1 parent fe72c72 commit c443806
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 7 deletions.
16 changes: 16 additions & 0 deletions src/renderers/createReactRenderer.spec.luau
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,22 @@ test("update the component on arg changes", function()
expect(button.Text).toBe("Enabled")
end)

test("renders primitives", function()
local story = createReactStory(React.createElement("TextLabel", {
Text = "Hello, World",
}))

render(container, story)

act(function()
task.wait()
end)

local label = container:FindFirstChildWhichIsA("TextLabel")
assert(label, "no TextLabel found")
expect(label.Text).toBe("Hello, World")
end)

test("lifecycle", function()
expect(#container:GetChildren()).toBe(0)

Expand Down
27 changes: 21 additions & 6 deletions src/renderers/createRoactRenderer.luau
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,35 @@ type Packages = {
Roact: any,
}

local function isRoactElement(maybeElement: any): boolean
if typeof(maybeElement) == "table" then
return maybeElement.component and maybeElement.props
end
return false
end

local function createRoactRenderer(packages: Packages): StoryRenderer<unknown>
local Roact = packages.Roact
local tree
local currentElement
local currentComponent

local function mount(container, story, props)
currentElement = story.story
local renderedElement = Roact.createElement(story.story, props)
tree = Roact.mount(renderedElement, container, "RoactRenderer")
local element

if isRoactElement(story.story) then
currentComponent = story.story.component
element = story.story
else
currentComponent = story.story
element = Roact.createElement(currentComponent, props)
end

tree = Roact.mount(element, container, "RoactRenderer")
end

local function update(props)
if tree and currentElement then
local element = Roact.createElement(currentElement, props)
if tree and currentComponent then
local element = Roact.createElement(currentComponent, props)
Roact.update(tree, element)
end
end
Expand Down
27 changes: 27 additions & 0 deletions src/renderers/createRoactRenderer.spec.luau
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,33 @@ test("update the component on arg changes", function()
expect(button.Text).toBe("Enabled")
end)

test("render a pre-created Roact element", function()
local function TextLabelStory()
return Roact.createElement("TextLabel", {
Text = "Hello, World",
})
end
local story = createRoactStory(Roact.createElement(TextLabelStory))

render(container, story)

local element = container:FindFirstChildOfClass("TextLabel")
assert(element, "no element found")
expect(element.Text).toBe("Hello, World")
end)

test("renders primitives", function()
local story = createRoactStory(Roact.createElement("TextLabel", {
Text = "Hello, World",
}))

render(container, story)

local label = container:FindFirstChildWhichIsA("TextLabel")
assert(label, "no TextLabel found")
expect(label.Text).toBe("Hello, World")
end)

test("lifecycle", function()
expect(#container:GetChildren()).toBe(0)

Expand Down
2 changes: 1 addition & 1 deletion wally.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "flipbook-labs/storyteller"
version = "0.3.0"
version = "0.4.0"
license = "MIT"
registry = "https://github.com/UpliftGames/wally-index"
realm = "shared"
Expand Down

0 comments on commit c443806

Please sign in to comment.