Documentation
¶
Overview ¶
Package primitives provides low-level layout primitives for ForgeUI
Primitives are the building blocks for more complex components. They provide type-safe wrappers around common CSS layout patterns.
All primitives only import from the root forgeui package to avoid circular dependencies. They should not import from other component packages.
Index ¶
- func Box(opts ...BoxOption) g.Node
- func Center(children ...g.Node) g.Node
- func Container(children ...g.Node) g.Node
- func Flex(opts ...FlexOption) g.Node
- func Grid(opts ...GridOption) g.Node
- func HStack(gap string, children ...g.Node) g.Node
- func Provider(opts ...ProviderOption) g.Node
- func ProviderDispatch(providerName, eventName, data string) string
- func ProviderMethod(providerName, method string, args ...string) string
- func ProviderScriptUtilities() g.Node
- func ProviderStack(providers ...g.Node) g.Node
- func ProviderValue(providerName, key string) string
- func Spacer() g.Node
- func Text(opts ...TextOption) g.Node
- func VStack(gap string, children ...g.Node) g.Node
- type BoxOption
- func WithAs(tag string) BoxOption
- func WithAttrs(attrs ...g.Node) BoxOption
- func WithBackground(bg string) BoxOption
- func WithChildren(children ...g.Node) BoxOption
- func WithClass(class string) BoxOption
- func WithHeight(h string) BoxOption
- func WithMargin(m string) BoxOption
- func WithPadding(p string) BoxOption
- func WithRounded(rounded string) BoxOption
- func WithShadow(shadow string) BoxOption
- func WithWidth(w string) BoxOption
- type BoxProps
- type FlexOption
- func FlexAlign(align string) FlexOption
- func FlexAttrs(attrs ...g.Node) FlexOption
- func FlexChildren(children ...g.Node) FlexOption
- func FlexClass(class string) FlexOption
- func FlexDirection(direction string) FlexOption
- func FlexGap(gap string) FlexOption
- func FlexJustify(justify string) FlexOption
- func FlexWrap(wrap string) FlexOption
- type FlexProps
- type GridOption
- func GridAttrs(attrs ...g.Node) GridOption
- func GridChildren(children ...g.Node) GridOption
- func GridClass(class string) GridOption
- func GridCols(cols int) GridOption
- func GridColsLG(cols int) GridOption
- func GridColsMD(cols int) GridOption
- func GridColsSM(cols int) GridOption
- func GridColsXL(cols int) GridOption
- func GridGap(gap string) GridOption
- type GridProps
- type ProviderOption
- func WithProviderAttrs(attrs ...g.Node) ProviderOption
- func WithProviderChildren(children ...g.Node) ProviderOption
- func WithProviderClass(class string) ProviderOption
- func WithProviderDebug(debug bool) ProviderOption
- func WithProviderHook(name, code string) ProviderOption
- func WithProviderInit(init string) ProviderOption
- func WithProviderMethods(methods string) ProviderOption
- func WithProviderName(name string) ProviderOption
- func WithProviderState(state map[string]any) ProviderOption
- type ProviderProps
- type TextOption
- func TextAlign(align string) TextOption
- func TextAs(tag string) TextOption
- func TextAttrs(attrs ...g.Node) TextOption
- func TextChildren(children ...g.Node) TextOption
- func TextClass(class string) TextOption
- func TextColor(color string) TextOption
- func TextSize(size string) TextOption
- func TextWeight(weight string) TextOption
- type TextProps
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Center ¶
Center creates a centered container using flexbox Centers both horizontally and vertically
func Container ¶
Container creates a responsive container with max-width constraints Commonly used for page layouts
func HStack ¶
HStack creates a horizontal stack (flex row) This is a convenience wrapper around Flex for horizontal layouts
func Provider ¶
func Provider(opts ...ProviderOption) g.Node
Provider creates a provider context component that shares state with children.
The Provider pattern enables deeply nested components to access shared state without prop drilling. It uses Alpine.js's x-data for scoped state and events for cross-component communication.
Example:
primitives.Provider(
primitives.WithProviderName("sidebar"),
primitives.WithProviderState(map[string]any{
"collapsed": false,
"mobileOpen": false,
}),
primitives.WithProviderMethods(`
toggle() {
this.collapsed = !this.collapsed;
this.$dispatch('sidebar:toggled', { collapsed: this.collapsed });
}
`),
primitives.WithProviderChildren(
// child components can access provider via $provider.sidebar
),
)
Children can access provider state using: - Alpine expressions: $el.closest('[data-provider="sidebar"]').__x.$data.collapsed - Helper function: getProvider($el, 'sidebar') - Magic property (if registered globally): $provider.sidebar
func ProviderDispatch ¶
ProviderDispatch returns an Alpine expression to dispatch a provider event. This is useful for cross-provider communication.
Example:
alpine.XClick(primitives.ProviderDispatch("sidebar", "toggle", "{ open: true }"))
func ProviderMethod ¶
ProviderMethod returns an Alpine expression to call a provider method.
Example:
alpine.XClick(primitives.ProviderMethod("sidebar", "toggle"))
// Generates: @click="$el.closest('[data-provider=\"sidebar\"]').__x.$data.toggle()"
func ProviderScriptUtilities ¶
ProviderScriptUtilities returns a script tag with helper functions for working with providers. Include this once in your page head or before Alpine.js initialization.
Provides global functions: - getProvider(el, name): Get provider state object - providerValue(el, name, key): Get specific provider state value - providerCall(el, name, method, ...args): Call provider method - providerDispatch(el, name, event, data): Dispatch provider event
func ProviderStack ¶
ProviderStack creates a nested stack of providers. This is useful when you need multiple providers in a component tree.
Example:
primitives.ProviderStack(
primitives.Provider(primitives.WithProviderName("theme"), ...),
primitives.Provider(primitives.WithProviderName("sidebar"), ...),
primitives.Provider(primitives.WithProviderName("auth"), ...),
)
func ProviderValue ¶
ProviderValue returns an Alpine expression to access a provider's state. Use this in x-bind, x-show, x-text, etc. to access provider state.
Example:
g.Attr("x-show", primitives.ProviderValue("sidebar", "collapsed"))
// Generates: x-show="$el.closest('[data-provider=\"sidebar\"]').__x.$data.collapsed"
Types ¶
type BoxOption ¶
type BoxOption func(*BoxProps)
BoxOption is a functional option for configuring Box
func WithBackground ¶
WithBackground sets background classes
func WithRounded ¶
WithRounded sets border-radius classes
type BoxProps ¶
type BoxProps struct {
As string // HTML tag (div, span, section, article, etc.)
Class string
P string // padding
M string // margin
Bg string // background
Rounded string // border-radius
Shadow string // box-shadow
W string // width
H string // height
Children []g.Node
Attrs []g.Node
}
BoxProps defines properties for the Box component
type FlexOption ¶
type FlexOption func(*FlexProps)
FlexOption is a functional option for configuring Flex
func FlexDirection ¶
func FlexDirection(direction string) FlexOption
FlexDirection sets the flex direction
type FlexProps ¶
type FlexProps struct {
Direction string // row, col, row-reverse, col-reverse
Wrap string // wrap, nowrap, wrap-reverse
Justify string // start, end, center, between, around, evenly
Align string // start, end, center, stretch, baseline
Gap string // gap size
Class string
Children []g.Node
Attrs []g.Node
}
FlexProps defines properties for the Flex component
type GridOption ¶
type GridOption func(*GridProps)
GridOption is a functional option for configuring Grid
type GridProps ¶
type GridProps struct {
Cols int // number of columns
ColsSM int // columns at sm breakpoint
ColsMD int // columns at md breakpoint
ColsLG int // columns at lg breakpoint
ColsXL int // columns at xl breakpoint
Gap string // gap size
Class string
Children []g.Node
Attrs []g.Node
}
GridProps defines properties for the Grid component
type ProviderOption ¶
type ProviderOption func(*ProviderProps)
ProviderOption is a functional option for configuring a Provider.
func WithProviderAttrs ¶
func WithProviderAttrs(attrs ...g.Node) ProviderOption
WithProviderAttrs adds custom HTML attributes to the provider wrapper.
func WithProviderChildren ¶
func WithProviderChildren(children ...g.Node) ProviderOption
WithProviderChildren sets the children that will have access to this provider.
func WithProviderClass ¶
func WithProviderClass(class string) ProviderOption
WithProviderClass adds custom CSS classes to the provider wrapper.
func WithProviderDebug ¶
func WithProviderDebug(debug bool) ProviderOption
WithProviderDebug enables debug mode for the provider.
func WithProviderHook ¶
func WithProviderHook(name, code string) ProviderOption
WithProviderHook adds a lifecycle hook to the provider. Common hooks: "onMount", "onUpdate", "onDestroy"
func WithProviderInit ¶
func WithProviderInit(init string) ProviderOption
WithProviderInit sets initialization code for the provider.
func WithProviderMethods ¶
func WithProviderMethods(methods string) ProviderOption
WithProviderMethods adds JavaScript methods to the provider. Methods have access to 'this' (the provider state) and Alpine magic properties.
Example:
WithProviderMethods(`
toggle() {
this.open = !this.open;
this.$dispatch('provider:toggled', { open: this.open });
}
`)
func WithProviderName ¶
func WithProviderName(name string) ProviderOption
WithProviderName sets the provider's unique identifier.
func WithProviderState ¶
func WithProviderState(state map[string]any) ProviderOption
WithProviderState sets the provider's initial state.
type ProviderProps ¶
type ProviderProps struct {
// Name is the unique identifier for this provider (e.g., "sidebar", "theme")
Name string
// State is the initial state as a map of key-value pairs
State map[string]any
// Methods is raw JavaScript code defining provider methods
// Methods will be merged with the state object and have access to 'this'
// Example: "toggle() { this.open = !this.open; }"
Methods string
// OnInit is JavaScript code that runs when the provider initializes
// Useful for setup logic, event listeners, etc.
OnInit string
// Class adds custom CSS classes to the provider wrapper
Class string
// Attrs adds custom HTML attributes to the provider wrapper
Attrs []g.Node
// Children are the child nodes that will have access to this provider
Children []g.Node
// Debug enables console logging for state changes
Debug bool
// Hooks define lifecycle hooks for the provider
Hooks map[string]string
}
ProviderProps defines the configuration for a Provider component.
type TextOption ¶
type TextOption func(*TextProps)
TextOption is a functional option for configuring Text
type TextProps ¶
type TextProps struct {
As string // HTML tag (p, span, div, h1-h6)
Size string // text size class
Weight string // font weight class
Color string // text color class
Align string // text alignment
Class string
Children []g.Node
Attrs []g.Node
}
TextProps defines properties for the Text component