Skip to content

Commit

Permalink
Fix hot reload min/max change breaking sliders (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boegie19 authored Feb 1, 2024
1 parent 07ce004 commit f59e163
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/widgets/slider.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ return Runtime.widget(function(options)

local min = options.min or 0
local max = options.max or 1
local value, setValue = Runtime.useState(options.initial or 0)
local initial = options.initial or 0
local initPercent = (initial - min) / (max - min)
local percentageValue, setPercentageValue = Runtime.useState(initPercent)

local refs = Runtime.useInstance(function(ref)
local connect = createConnect()
Expand Down Expand Up @@ -71,7 +73,7 @@ return Runtime.widget(function(options)

local percent = x / maxPos

setValue(percent * (max - min) + min)
setPercentageValue(percent)
end)
end,

Expand All @@ -92,8 +94,8 @@ return Runtime.widget(function(options)
end)

local maxPos = refs.frame.AbsoluteSize.X - refs.frame.dot.AbsoluteSize.X
local percent = (value - min) / (max - min)
refs.frame.dot.Position = UDim2.new(0, percent * maxPos + refs.frame.dot.AbsoluteSize.X / 2, 0.5, 0)
refs.frame.dot.Position = UDim2.new(0, percentageValue * maxPos + refs.frame.dot.AbsoluteSize.X / 2, 0.5, 0)

local value = percentageValue * (max - min) + min
return value
end)

0 comments on commit f59e163

Please sign in to comment.