Play
- Package
- purescript-play
- Repository
- shamansir/purescript-play
The Play API provides a way to define declarative layout for creating flexible, responsive user interfaces in PureScript.
It is inspired by Clay UI layouting system for C++ by Nic Baker. It inherits its simple yet powerful design.
The API supports:
- Flexbox-like layout with direction control (horizontal/vertical)
- Size constraints (fixed, fit-to-content, grow-to-fill, ...)
- Padding and gaps between elements
- Automatic size calculation and positioning
#Play Source
newtype Play aA layout tree containing elements of type a with layout definitions.
This is the primary type for building layouts before they are computed.
Each element contains:
- User data of type
a - Layout definition (sizing, padding, direction, etc.)
- Zero or more child elements
The only required properties for an element to be visible are the way its width / height calculated.
Example:
myLayout
= P.i "content"
~* P.widthGrow
~* P.height 100.0
~* P.padding (P.all 5.0)
~* P.with [ P.i "child1", P.i "child2" ]
:: Play String
See more examples in the README and Test/Demo/Examples in the source.
Instances
#Layout Source
newtype Layout aA computed layout tree with final positioning and sizes.
This is the result of running layout computation on a Play definition using Play.layout function.
Each element contains:
- User data of type
a - Layout definition (preserved for potential rollback)
- Computed rectangular bounds (position and size)
Instances
#with Source
with :: forall a. Array (Play a) -> Play a -> Play aAdd additional children to an existing element. The new children are appended to the existing children list.
Example:
container = P.i "base"
~* P.with [child1, child2]
container2 = P.p "base" [child1, child2]
~* P.with [child3, child4] -- Now has 4 children total
#paddingTop Source
paddingTop :: forall a. Number -> Play a -> Play aSet padding for the top side only. Other padding values remain unchanged.
#paddingLeft Source
paddingLeft :: forall a. Number -> Play a -> Play aSet padding for the left side only. Other padding values remain unchanged.
#paddingBottom Source
paddingBottom :: forall a. Number -> Play a -> Play aSet padding for the bottom side only. Other padding values remain unchanged.
#paddingRight Source
paddingRight :: forall a. Number -> Play a -> Play aSet padding for the right side only. Other padding values remain unchanged.
#p Source
p :: forall a. a -> Array (Play a) -> Play aCreate a layout element that has children (parent).
Parameters:
a: The user data/content for this elementArray (Play a): Array of child elements
The created element uses the default layout definition, which can be
modified using property functions and the ~* operator.
Example:
container = P.p "header"
[ P.i "logo"
, P.i "navigation"
]
#i Source
i :: forall a. a -> Play aCreate a leaf layout element with no children (item).
This is the primary constructor for creating layout hierarchies and
a convenience function equivalent to P.p a [], the children could
be added later using ~* P.with [ child1, child2, ... ] .
Example:
element = P.i "Hello World"
#fromTree Source
fromTree :: forall a. Tree (WithDef a) -> Play aWrap a tree structure as a Play layout.
This is the inverse of toTree and is useful when constructing Play layouts
from external tree structures.
Takes the compiled elements definitions and wraps them in a Play definition (internal representation is the very same Tree).
#layout Source
layout :: forall a. Play a -> Layout aCompute the final layout from a Play tree specification. This performs a three-phase layout computation:
- Fit Sizing: Calculate minimum required sizes based on content and constraints
- Grow Sizing: Distribute remaining space to elements with grow constraints
- Positioning: Calculate final positions for all elements
The result is a Layout containing all the elements with their computed bounds (positions and rectangles).
Example:
myPlay = Play.i "root" ~* Play.with [ Play.i "child1", Play.i "child2" ] :: Play String
computedLayout = Play.layout myPlay :: Layout String
#layoutToTree Source
layoutToTree :: forall a. Layout a -> Tree (WithRect a)Extract the tree structure from a Layout, removing layout definitions. The result contains only the values and computed rectangular bounds. This is useful for rendering with keeping hierarchy or hit-testing operations.
#flattenLayout Source
flattenLayout :: forall a. Layout a -> Array (WithRect a)Flatten a Layout into an array of all elements with their computed bounds. This provides a simple list of all elements for rendering or processing. The tree hierarchy is flattened but positioning information is preserved, so it is completely safe to iterate the resulting array and place the elements in the corresponding bounds. The parents are followed by their children, so z-index is also preserved.
#rollback Source
rollback :: forall a. Layout a -> Play aConvert a computed Layout back to a Play specification. This removes the computed positioning information but preserves the layout definitions, allowing for re-computation with modifications.
Example:
myPlay = P.i "root" ~* P.with [ P.i "child1", P.i "child2" ] :: Play String
computedLayout = P.layout myPlay :: P.Layout String
originalPlay = P.rollback computedLayout :: Play String
modifiedPlay = originalPlay ~* P.padding (P.all 5.0) :: Play String
newLayout = P.layout modifiedPlay :: P.Layout String
#layoutSize Source
layoutSize :: forall a. Layout a -> SizeGet the total size of the root element in a computed Layout. This represents the minimum bounding box containing all elements.
#widthFitGrow Source
widthFitGrow :: forall (a :: Type). Play a -> Play aSet width to fit content but grow if extra space is available.
#heightGrow Source
heightGrow :: forall (a :: Type). Play a -> Play aSet height to grow and fill available vertical space.
#heightFitGrow Source
heightFitGrow :: forall (a :: Type). Play a -> Play aSet height to fit content but grow if extra space is available.
#topToBottom Source
topToBottom :: forall (a :: Type). Play a -> Play aSet layout direction to arrange children vertically (top to bottom).
#leftToRight Source
leftToRight :: forall (a :: Type). Play a -> Play aSet layout direction to arrange children horizontally (left to right). This is the default direction.
#(~*) Source
Operator alias for Play.playProp (left-associative / precedence 1)
Property application operator for chaining layout modifications.
This is an alias for flipped function application just to distinguish layout-related calls from other ones.
So it could be replaces with (#) operator at any place.
See the example of usage at Play a type above.
Re-exports from Play.Types
#WithDefSize Source
type WithDefSize a = { def :: Def, size :: Size, v :: a }An element with layout definition and computed size. Intermediate state during layout computation.
#WithDefRect Source
type WithDefRect a = { def :: Def, rect :: Rect, v :: a }An element with both layout definition and computed rectangular bounds. Contains complete layout information for potential rollback operations.
#Sizing Source
data SizingDefines how an element should size itself along a particular axis.
None: No specific sizing constraintFixed Number: Fixed size in pixelsFit: Size to fit the content (minimum required space)Grow: Expand to fill available spaceFitGrow: If fitting the content requires more space than growing, then fit anyway, else grow to fill the available space
Constructors
Instances
- Modules
- Play
- Play.
Extra - Play.
Types
FixedPct Percentage FitMin { min :: Number } FitMax { max :: Number } FitMinMax { min :: Number, max :: Number }