Module:Box
Jump to navigation
Jump to search
| Module Documentation [edit] |
|---|
|
Generic box with automatic dark mode based on tint colors. FunctionsSelf-Contained Box{{#invoke:Box|main
|background=false
|body=Body
|compact=false
|outer=false
|tintColor=#f8f2fb
|vfill=false
|vspace=false
}}
Use
Open/Close Box{{#invoke:Box|open
|background=false
|compact=false
|outer=false
|tintColor=#f8f2fb
|vfill=false
|vspace=false
}}
Body
{{#invoke:Box|close
|outer=false
}}
Use
ExamplesPlain Box{{#invoke:Box|main|body=Body}}
Body {{#invoke:Box|main|body=Body|tintColor=#f8f2fb}}
Body {{#invoke:Box|main|body=Body|tintColor=#e4efff}}
Body {{#invoke:Box|open|tintColor=#e4efff|compact=true}}
{{#invoke:Box|main|body=Title|background=true|tintColor=#e4efff}}
Body
{{#invoke:Box|close}}
Title Body Outer Box{{#invoke:Box|main|body=Body|outer=true}}
Body {{#invoke:Box|main|body=Body|outer=true|tintColor=#f8f2fb}}
Body {{#invoke:Box|main|body=Body|outer=true|tintColor=#e4efff}}
Body {{#invoke:Box|open|tintColor=#e4efff|outer=true|compact=true}}
{{#invoke:Box|main|body=Title|background=true|tintColor=#e4efff}}
Body
{{#invoke:Box|close}}
Title Body |
local getArgs = require('Module:Arguments').getArgs
local toBoolean = require('Module:Boolean').toBoolean
local color = require('Module:Color')
local p = {}
--------------------------------------------------------------------------------
-- PRIVATE HELPER FUNCTIONS
--------------------------------------------------------------------------------
-- Builds the opening HTML tags.
local function buildOpeningHtml(args, frame)
local tintColor = args.tintColor
if tintColor == nil or tintColor == "" then tintColor = "#dddddd" end
local borderColor = color.darken(tintColor)
local htmlParts = {}
table.insert(htmlParts, tostring(
frame:extensionTag{
name = 'templatestyles',
args = { src = 'Module:Box/base.css' }
}
))
-- Root
local boxClasses = {}
table.insert(boxClasses, 'pm-box')
if toBoolean(args.vfill) then
table.insert(boxClasses, 'pm-box--vfill')
end
if toBoolean(args.vspace) then
table.insert(boxClasses, 'pm-box--vspace')
end
table.insert(htmlParts, string.format(
'<div class="%s">',
table.concat(boxClasses, ' ')
))
-- Outer
if toBoolean(args.outer) then
local outerClasses = {}
table.insert(outerClasses, 'pm-box__outer')
if toBoolean(args.vfill) then
table.insert(outerClasses, 'pm-box__outer--vfill')
end
table.insert(htmlParts, string.format(
'<div class="%s" style="%s">',
table.concat(outerClasses, ' '),
string.format(
'border-color: light-dark(%s, %s);',
tintColor,
color.lighten(color.invert(tintColor), 3)
)
))
end
-- Inner
local innerClasses = {}
table.insert(innerClasses, 'pm-box__inner');
if toBoolean(args.compact) then
table.insert(innerClasses, 'pm-box__inner--compact')
end
if toBoolean(args.vfill) then
table.insert(innerClasses, 'pm-box__inner--vfill')
end
local innerStyles = {}
table.insert(innerStyles, string.format(
'border-color: light-dark(%s, %s);',
borderColor,
color.invert(borderColor)
))
if toBoolean(args.background) then
table.insert(innerStyles, string.format(
'background-color: light-dark(%s, %s);',
tintColor,
color.invert(tintColor)
))
end
table.insert(htmlParts, string.format(
'<div class="%s" style="%s">',
table.concat(innerClasses, ' '),
table.concat(innerStyles, ' ')
))
-- Body
table.insert(htmlParts, '<div class="pm-box__body">')
-- Return
return table.concat(htmlParts, '')
end
-- Builds the closing HTML tags.
local function buildClosingHtml(args)
if toBoolean(args.outer) then
return '</div></div></div></div>' -- Closes body, inner, outer, root
else
return '</div></div></div>' -- Closes body, inner, root
end
end
--------------------------------------------------------------------------------
-- PUBLIC FUNCTIONS
--------------------------------------------------------------------------------
-- Self-contained boxes: {{#invoke:Box|main|...}}
function p.main(frame)
local args = getArgs(frame)
return buildOpeningHtml(args, frame) .. (args.body or "") .. buildClosingHtml(args)
end
-- Open a box for wrapping: {{#invoke:Box|open|...}}
function p.open(frame)
local args = getArgs(frame)
return buildOpeningHtml(args, frame)
end
-- Close a box: {{#invoke:Box|close|...}}
function p.close(frame)
local args = getArgs(frame)
return buildClosingHtml(args)
end
return p