Skip to content

Instantly share code, notes, and snippets.

@cxmeel
cxmeel / Store.luau
Last active September 15, 2024 22:15
export type Value<T> =
((value: (T | (current: T) -> T)?, bypassSignals: boolean?) -> T)
& {
changed: RBXScriptSignal<T, T>,
initialValue: T,
destroy: () -> (),
reset: () -> (),
onChange: (callback: (prev: T, next: T) -> (), runOnInit: boolean?) -> (),
onReset: (() -> ())?,
}
@cxmeel
cxmeel / useLatest.luau
Created September 4, 2024 13:29
useSelector hook for BasicState
local React = require("@pkgs/React")
local useMemo = React.useMemo
local useRef = React.useRef
type Predicate<T = unknown> = (a: T?, b: T) -> boolean
local function isStrictEqual(a: unknown?, b: unknown)
return a == b
end
@cxmeel
cxmeel / fromInstance.luau
Created August 19, 2024 21:27
Generates a React component from an existing Instance. Alternatively, set `clone = false` to "inverse portal."
local React = require("@pkgs/React")
local useEffect = React.useEffect
local e = React.createElement
local function applyProps(target: Instance, props, applyRef)
for propName, propValue in props do
if propName == "children" or propName == "Parent" then
continue
end
local calculateScale = require(calculateScale)

local currentScale = calculateScale(myFrame)
local frameSize = frame.AbsoluteSize * currentScale

print(frameSize)
@cxmeel
cxmeel / useAttribute.luau
Last active September 4, 2024 13:30
React Luau hooks
local React = require("@pkgs/React")
local useState = React.useState
local useEffect = React.useEffect
local function useAttribute<T>(
target: Instance,
attributeName: string,
fallbackValue: T?
): T?
@cxmeel
cxmeel / README.StoryCanvas.md
Last active May 20, 2024 11:17
StoryCanvas is a component for rendering React components in Hoarcekat.

StoryCanvas

StoryCanvas is a component for rendering React components in Hoarcekat.

Screenshot of StoryCanvas being rendered in Hoarcekat

A StoryCanvas component handles mounting and unmounting of stories automatically. It features:

  • A checkered background (synced with Studio theme)
  • A UIListLayout to display components centrally and evenly spaced
@cxmeel
cxmeel / lune-build.lua
Last active May 9, 2024 11:58
A Lune script for building plugins and watching for file changes
--!strict
--[[
Usage:
Build production release:
> `lune run build`
Build dev release to Roblox/Plugins directory:
> `lune run build -- --dev`
--[[
Parses a ISO 8601 UTC offset string into a float.
parseIsoUtcOffset("-0500") --> -5
parseIsoUtcOffset("+02") --> 2
parseIsoUtcOffset("-04:30") --> -4.5
parseIsoUtcOffset("Z") --> 0
local localTimezoneOffset = os.date("%z")
parseIsoUtcOffset(localTimezoneOffset)
@cxmeel
cxmeel / useDpiScale.lua
Last active August 19, 2024 21:33
React-lua hook for determining DPI scale.
local UserInputService = game:GetService("UserInputService")
local React = require(script.Parent.Parent.Packages.React)
local useState = React.useState
local useEffect = React.useEffect
local function calculateDpiScale()
local BUTTON_CONTENTID = UserInputService:GetImageForKeyCode(Enum.KeyCode.ButtonA)
local BUTTON_SCALE = BUTTON_CONTENTID:lower():match("@([%d%.]-)x%.%w+$")
@cxmeel
cxmeel / timezones.lua
Last active April 11, 2024 21:56
A Lua table of alpha-2 region codes to UTC offsets in that region.
--[[
Format:
```lua
REGION_CODE = {
{ STD, DST },
}
```
]]
return {