Skip to content

Instantly share code, notes, and snippets.

@howmanysmall
Created September 10, 2019 18:16
Show Gist options
  • Select an option

  • Save howmanysmall/3ed6a9bd086f251aaf2a10df5323b100 to your computer and use it in GitHub Desktop.

Select an option

Save howmanysmall/3ed6a9bd086f251aaf2a10df5323b100 to your computer and use it in GitHub Desktop.
local RunService = game:GetService 'RunService'
local TextService = game:GetService 'TextService'
local Root = script.Parent.Parent
local Libraries = Root:WaitForChild 'Libraries'
local Vendor = Root:WaitForChild 'Vendor'
local UI = Root:WaitForChild 'UI'
-- Constants
local EMPTY_VECTOR2 = Vector2.new()
-- Roact
local Roact = require(Vendor:WaitForChild 'Roact')
local new = Roact.createElement
local Frame = require(UI:WaitForChild 'Frame')
local TextLabel = require(UI:WaitForChild 'TextLabel')
-- Create component
local Tooltip = Roact.PureComponent:extend 'Tooltip'
-- Set defaults
Tooltip.defaultProps = {
AnchorPoint = Vector2.new(0.5, 0),
BackgroundColor3 = Color3.new(0, 0, 0),
BackgroundTransparency = 0.4,
BorderSizePixel = 0,
Position = UDim2.new(0.5, 0, 1, 3),
ZIndex = 10,
Font = Enum.Font.GothamBold,
TextColor3 = Color3.new(1, 1, 1),
TextSize = 10,
ShowDelay = 0
}
function Tooltip:init(props)
local ShowDelay = props.ShowDelay
self:setState {Visible = false}
self.IsHovered = false
self.ConnectHover = function()
self.HoverConnection = RunService.Heartbeat:Connect(function()
if self.IsHovered and tick() >= self.TargetTime then
self.DisconnectHover()
self:setState {Visible = true}
end
end)
end
self.DisconnectHover = function()
if self.HoverConnection then
self.HoverConnection:Disconnect()
end
end
self.InputBegan = function(_, InputObject)
if InputObject.UserInputType == Enum.UserInputType.MouseMovement then
self.IsHovered = true
self.TargetTime = tick() + ShowDelay
self.ConnectHover()
end
end
self.InputEnded = function(_, InputObject)
if InputObject.UserInputType == Enum.UserInputType.MouseMovement then
self.IsHovered = false
self.TargetTime = 0
self.DisconnectHover()
self:setState {Visible = false}
end
end
end
function Tooltip:willUnmount()
self.DisconnectHover()
end
function Tooltip:render()
local Properties = self.props
local Text = Properties.Text
local TextSize = Properties.TextSize
local Font = Properties.Font
local FrameSize = TextService:GetTextSize(Text, TextSize, Font, EMPTY_VECTOR2)
return new(Frame, {
[Roact.Event.InputBegan] = self.InputBegan,
[Roact.Event.InputEnded] = self.InputEnded
}, {
Tooltip = new(TextLabel, {
AnchorPoint = Properties.AnchorPoint,
Position = Properties.Position,
Size = UDim2.new(0, FrameSize.X + 10, 0, FrameSize.Y + 10),
Visible = self.state.Visible,
BackgroundColor3 = Properties.BackgroundColor3,
BackgroundTransparency = Properties.BackgroundTransparency,
BorderSizePixel = Properties.BorderSizePixel,
Font = Font,
Text = Text,
TextColor3 = Properties.TextColor3,
TextSize = TextSize,
ZIndex = Properties.ZIndex,
TextXAlignment = Enum.TextXAlignment.Center
})
})
end
return Tooltip
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment