StyleSheet
Aggregates StyleRules and can be linked to DataModel trees to apply style properties to instances. Note that a StyleSheet may exist outside the DataModel, but it cannot be derived or linked to a DataModel tree in such a case.
Summary
Methods
Returns an array of other StyleSheets from which the StyleSheet is deriving StyleRules and token definitions.
Sets the StyleSheet to derive StyleRules and token definitions from one or more other StyleSheets.
Returns an array of associated StyleRules.
Inserts a new StyleRule into the array of rules.
Similar to InsertStyleRule() but lets you declare and set multiple StyleRules at once.
Events
Events inherited from StyleBaseFires when one or more StyleRules is explicitly changed on the connected StyleSheet or StyleRule.
Properties
Methods
GetDerives
Returns an array of other StyleSheets from which the StyleSheet is deriving StyleRules and token definitions.
Returns
Array of other StyleSheets.
SetDerives
Sets the StyleSheet to derive StyleRules and token definitions from one or more other StyleSheets in the order they are listed. This method spawns the appropriate StyleDerive instances and sets their priorities to establish the specified derivations.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local coreSheet = ReplicatedStorage:FindFirstChild("CoreSheet")
-- Create a tokens style sheet
local tokensSheet = Instance.new("StyleSheet")
tokensSheet.Name = "Tokens"
tokensSheet.Parent = ReplicatedStorage
-- Set tokens (attributes) on tokens sheet
tokensSheet:SetAttribute("LightGray", Color3.new(0.9, 0.9, 0.9))
tokensSheet:SetAttribute("DarkGray", Color3.new(0.2, 0.2, 0.2))
-- Create theme style sheets
local lightThemeSheet = Instance.new("StyleSheet")
lightThemeSheet.Name = "LightTheme"
lightThemeSheet:SetAttribute("Background", "$LightGray")
lightThemeSheet.Parent = ReplicatedStorage
local darkThemeSheet = Instance.new("StyleSheet")
darkThemeSheet.Name = "DarkTheme"
darkThemeSheet:SetAttribute("Background", "$DarkGray")
darkThemeSheet.Parent = ReplicatedStorage
-- Set theme sheets to derive from tokens sheet
lightThemeSheet:SetDerives({ tokensSheet })
darkThemeSheet:SetDerives({ tokensSheet })
local themeDerive = Instance.new("StyleDerive")
themeDerive.Parent = coreSheet
themeDerive.StyleSheet = lightThemeSheet
-- Function to dynamically change the derived theme for the core sheet
local function changeTheme()
if themeDerive.StyleSheet == lightThemeSheet then
themeDerive.StyleSheet = darkThemeSheet
elseif themeDerive.StyleSheet == darkThemeSheet then
themeDerive.StyleSheet = lightThemeSheet
end
end
Note that if you've created a design using the Style Editor, the StyleSheet sheet in the Design folder of ReplicatedStorage will contain a StyleDerive to the BaseStyleSheet also in the Design folder. When setting derives with SetDerives(), be sure to include the base style sheet in the spot of least priority in relation to other StyleSheets in the derives array.
Parameters
Array of other StyleSheets to derive StyleRules and token definitions from.