feat(responsive): surface media hints on variant containers
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
2d8ddb4523
commit
fc4bae09cc
2 changed files with 20 additions and 0 deletions
|
|
@ -43,6 +43,7 @@ func (r *Responsive) Variant(name string, layout *Layout) *Responsive {
|
|||
|
||||
// Add registers a responsive variant. The optional media argument carries a
|
||||
// CSS media-query hint for downstream CSS generation (e.g. "(min-width: 768px)").
|
||||
// When supplied, Render emits it on the container as data-media.
|
||||
//
|
||||
// Usage example: NewResponsive().Add("desktop", NewLayout("HLCRF"), "(min-width: 1024px)")
|
||||
func (r *Responsive) Add(name string, layout *Layout, media ...string) *Responsive {
|
||||
|
|
@ -79,6 +80,10 @@ func (r *Responsive) renderWithLayoutPath(ctx *Context, path string) string {
|
|||
|
||||
b.WriteString(`<div data-variant="`)
|
||||
b.WriteString(escapeAttr(v.name))
|
||||
if v.media != "" {
|
||||
b.WriteString(`" data-media="`)
|
||||
b.WriteString(escapeAttr(v.media))
|
||||
}
|
||||
b.WriteString(`">`)
|
||||
b.WriteString(renderWithLayoutPath(v.layout, ctx, path))
|
||||
b.WriteString(`</div>`)
|
||||
|
|
|
|||
|
|
@ -19,6 +19,21 @@ func TestResponsive_SingleVariant_Good(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestResponsive_Add_MediaHint_Good(t *testing.T) {
|
||||
ctx := NewContext()
|
||||
r := NewResponsive().
|
||||
Add("desktop", NewLayout("C").C(Raw("content")), "(min-width: 1024px)")
|
||||
|
||||
got := r.Render(ctx)
|
||||
|
||||
if !containsText(got, `data-variant="desktop"`) {
|
||||
t.Fatalf("responsive should still contain data-variant, got:\n%s", got)
|
||||
}
|
||||
if !containsText(got, `data-media="(min-width: 1024px)"`) {
|
||||
t.Fatalf("responsive should expose media hint, got:\n%s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResponsive_MultiVariant_Good(t *testing.T) {
|
||||
ctx := NewContext()
|
||||
r := NewResponsive().
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue