Skip to content
chteauPublic

About

A simple module to manage Roblox's UI components inspired by React.

Resources

Stars

3 stars

Watchers

0 watching

Forks

Latest commit

Β 

History

59 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation


Fragment.luau



Banner

Luau Badge

Fragment is a simple module I made to manage Roblox's imperative UI instance system inspired by React. It currently supports state management, effects, declarative rendering and instance creation, spring-based animation, global stores, re-usable components, and Storybook-style previewing (Flipbook/UI Labs compatible) which allow you to build reactive user interfaces whilst still using default Roblox's UI components.

To learn more or get more details on the installation and available methods, read the docs here.

Installation

Via Wally (recommended)

Fragment is published on the Wally registry. Add it to your wally.toml:

[dependencies]
Fragment = "chteau/fragment@0.1.0"

Then run wally install. Fragment installs under ReplicatedStorage.Packages.Fragment (or wherever your Packages folder is synced), so require it from there:

local Fragment = require(ReplicatedStorage.Packages.Fragment)

Manually

  1. Insert a ModuleScript named Fragment into ReplicatedStorage.
  2. Paste the source code of Fragment into the ModuleScript you just created.
  3. Created a folder for your UI (e.g., MySuperDuperUI) somwehere like in StarterPlayerScripts.

And there you go, you got Fragment in your project! πŸ—£οΈ

Setting-Up Fragment

Once you installed Fragment and created the folder (e.g., MySuperDuperUI), you may now start using it. For your projects, I recommend using this structure:

.
└── StarterPlayerScripts/
    └── MySuperDuperUI/
        β”œβ”€β”€ FragmentLoad.client.luau -- This will be used to init a Fragment singleton instance
        β”œβ”€β”€ Components/
        β”‚   └── ...
        β”œβ”€β”€ Handles/
        β”‚   └── ...
        β”œβ”€β”€ Hooks/
        β”‚   └── ...
        └── Stores/
            └── ...

Now, in FragmentLoad.client.luau :: LocalScript you can now directly require the Fragment ModuleScript you installed and load your handles.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Fragment = require(ReplicatedStorage.Fragment)

-- Load all your modulescripts here!
Fragment.load({
    Stores = script.Parent.Stores,
    Contexts = script.Parent.Contexts,
    Hooks = script.Parent.Hooks,
    Components = script.Parent.Components,
    Handles = script.Parent.Handles,
})

That's it! You can see a bit of the API references and some examples in this readme or you can head to the Github Wiki to find out how it works in details.

Core Concepts

Fragment runs as a singleton across your whole client and can be required everywhere in your Handles, Stores, Hooks and Contexts.

1. Handles

Handles bind logic to specific UI instances. They are created using Fragment.newHandle and define a Rendered Function that runs whenever the state of your handle changes.

local Handle = Fragment.newHandle("MyButton", { "ScreenGui", "Frame", "TextButton" })

return Handle(function(Element: TextButton)
  element.Text = "Click me"

  Handle.Connect("Click", element.Activated, function()
    print("Button clicked")
  end)
end

2. Local State (bind)

You can manage your local handle's state similar to React's useState hook.

local count, setCount = Handle:bind(0)

Handle.Connect("Increment", button.Activated, function()
  setCount(count() + 1)
end)

element.Text = `Count: {count()}`

3. Global Stores (createStore)

You can also create centralized state management stores accessible across multiple handles.

-- Definition
return Fragment.createStore("PlayerData", function(set, get)
  return {
    Coins = 0,
    addCoins = function(amount: number)
      set({ Coins = get().Coins + amount })
    end
  }
end)

-- Usage in a Handle
local store = Fragment.useStore("PlayerData")
element.Text = `Coins: {store.Coins}`

This way of creating stores is really similar to Zustand (for React), and I did inspire myself from it to make this.

4. Context and Custom Hooks (compose)

You can share logic between handles using what's called Contexts and Composable functions.

-- New Context
local WindowContext = Fragment.createContext({ active = nil })

-- Custom Hook
local useWindow = Fragment.compose(function(handle, windowName)
  local ctx = handle:pull(WindowContext)
  local isOpen = handle:derive(function()
    return ctx.active == windowName
  end, { ctx.active })

  return isOpen, ctx
end)

5. Re-usable Components

You can finally create re-usable components you can call through your different handles.

-- New Component
local Component = Fragment.newComponent("ListItem", { "ScreenGui", "Assets", "ListItem" })

return Component(function(element, props)
     element.Text = props.Price
end)
local Handle = Fragment.newHandle(..., ...)

return Handle(function(element)
    local Store = Fragment.useStore(...)
    local Data = Store.getData()

    local Container = Handle.RegisterContainer(...)
    for _, entry in Data.Entries do
        local Item = Fragment.useComponent(name, { Price = math.random() }) -- Automatically returns a clone
        local live = Container.UpsertChild(Item)
        -- ...
    end
end)

6. Declarative Creation (Fragment.create)

You don't need a hand-placed Studio template for everything β€” Fragment.create builds a real Instance from code, with properties, events, and children in one call.

local button = Fragment.create("TextButton", {
  Text = "Click me",
  [Fragment.OnEvent("Activated")] = function()
    print("clicked")
  end,
  [Fragment.Children] = {
    Title = Fragment.create("TextLabel", { Text = "Hello" }),
  },
})

7. Lists (container.Each)

RegisterContainer already diffs children by key β€” Each wraps that loop so you don't write it by hand every time.

local list = Handle.RegisterContainer(element.ScrollFrame)

list.Each(data.Entries, function(entry) return entry.Id end, function(entry)
  return Fragment.create("TextLabel", { Name = entry.Id, Text = entry.Name })
end)

8. Motion (handle:spring)

Springs animate toward a target with real damped physics instead of jumping straight there β€” no manual Tween object to manage.

local size, setSize = Handle:spring(UDim2.fromOffset(100, 40), { frequency = 4, damping = 1 })

Handle.Connect("Hover", element.MouseEnter, function()
  setSize(UDim2.fromOffset(120, 48))
end)

element.Size = size()

9. Mounting Anywhere (Fragment.mount)

newHandle resolves a fixed path under PlayerGui. Fragment.mount renders straight into any Instance you already have β€” a tooltip, a dynamically created ScreenGui, anything that isn't a permanent screen. Call :Unmount() when you're done; it tears down effects, connections, and store/context subscriptions for you.

local handle = Fragment.mount(someFrame, function(element)
  element.BackgroundTransparency = 0
end)

-- later
handle:Unmount()

This is also what makes Fragment components previewable in Flipbook and UI Labs β€” see Fragment.Integrations.Flipbook / Fragment.Integrations.UILabs, and the Storybook guide in the docs.

10. Replicated Attributes (handle:attribute)

A lighter alternative to wiring up a full store for values the engine already replicates for you β€” reads a Roblox Attribute and re-renders when it changes, regardless of whether the server sets it directly, via ReplicaService, Knit, or anything else.

local getCoins = Handle:attribute(player, "Coins")
element.Text = `Coins: {getCoins()}`

Possible Usages

You can pretty much use Fragment for anything. In my case, I use it with ReplicaService and Knit. I really like react and since most of the people I work with don't use it's luau version and bake the UI directly using Roblox's Studio interface manager (if I may call it like that), I thought it would be pretty cool to have such a ressource out there; maybe there's other more complete frameworks for that on roblox but I didn't find any so, here it is.

Conclusion

Again, if you wanna know how things work in more details, check out the Github Wiki for API references and usage examples. Peace!

About

A simple module to manage Roblox's UI components inspired by React.

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Sponsor this project

Packages

Contributors

Languages