Add a customisable shadow to canvas boxes
Test and deploy / test (push) Successful in 2m11s
Test and deploy / deploy (push) Successful in 1m21s
Weekly Maintenance / maintenance (push) Successful in 6m55s

A box can now cast a shadow, independent of its frame: ticking "Shadow" in
the box editor gives a hard offset shadow, and Hard/Soft/Glow presets or any
typed CSS box-shadow value customise it. The value is stored on Box and
Component and carried through duplicates, components and layouts.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-23 23:18:35 +01:00
co-authored by Claude Opus 5.5
parent e764078aaf
commit 8bf70c98e8
15 changed files with 243 additions and 19 deletions
+6
View File
@@ -36,6 +36,9 @@ func (r *mutationResolver) CreateBox(ctx context.Context, input model.CreateBoxI
if input.Framed != nil {
box.Framed = *input.Framed
}
if input.Shadow != nil {
box.Shadow = *input.Shadow
}
if input.HideOnMobile != nil {
box.HideOnMobile = *input.HideOnMobile
}
@@ -102,6 +105,9 @@ func (r *mutationResolver) UpdateBox(ctx context.Context, id int, input model.Up
if input.Framed != nil {
box.Framed = *input.Framed
}
if input.Shadow != nil {
box.Shadow = *input.Shadow
}
if input.HideOnMobile != nil {
box.HideOnMobile = *input.HideOnMobile
}
+20
View File
@@ -6,6 +6,8 @@ package graph
import (
"errors"
"fmt"
"strings"
"adam-french.co.uk/backend/models"
)
@@ -14,6 +16,10 @@ import (
// be grabbed again on the canvas.
const minBoxSize = 40
// maxShadowLen bounds a box's shadow, which is a single CSS value: even a
// stack of a few comma-separated shadows is well under it.
const maxShadowLen = 200
func setInt(dst *int, src *int) {
if src != nil {
*dst = *src
@@ -43,9 +49,23 @@ func validateBox(box *models.Box) error {
if box.ComponentID != nil && box.Kind != "html" {
return errors.New("only an html box can be linked to a component")
}
if err := validateShadow(&box.Shadow); err != nil {
return err
}
box.X = max(box.X, 0)
box.Y = max(box.Y, 0)
box.W = max(box.W, minBoxSize)
box.H = max(box.H, minBoxSize)
return nil
}
// validateShadow trims a box-shadow value and bounds its length. The value is
// not parsed: the front end sets it through a style binding, so it can only
// ever be the one property, and an invalid value just draws no shadow.
func validateShadow(shadow *string) error {
*shadow = strings.TrimSpace(*shadow)
if len(*shadow) > maxShadowLen {
return fmt.Errorf("a shadow is at most %d characters", maxShadowLen)
}
return nil
}
+13 -1
View File
@@ -38,11 +38,17 @@ func (r *mutationResolver) CreateComponent(ctx context.Context, input model.Crea
if input.Framed != nil {
c.Framed = *input.Framed
}
if input.Shadow != nil {
c.Shadow = *input.Shadow
}
if input.AdminOnly != nil {
c.AdminOnly = *input.AdminOnly
}
c.W = max(c.W, minBoxSize)
c.H = max(c.H, minBoxSize)
if err := validateShadow(&c.Shadow); err != nil {
return nil, err
}
if err := r.Store.DB.Create(&c).Error; err != nil {
return nil, err
}
@@ -75,11 +81,17 @@ func (r *mutationResolver) UpdateComponent(ctx context.Context, id int, input mo
if input.Framed != nil {
c.Framed = *input.Framed
}
if input.Shadow != nil {
c.Shadow = *input.Shadow
}
if input.AdminOnly != nil {
c.AdminOnly = *input.AdminOnly
}
c.W = max(c.W, minBoxSize)
c.H = max(c.H, minBoxSize)
if err := validateShadow(&c.Shadow); err != nil {
return nil, err
}
if err := r.Store.DB.Save(&c).Error; err != nil {
return nil, err
}
@@ -128,7 +140,7 @@ func (r *mutationResolver) SaveBoxAsComponent(ctx context.Context, id int, name
}
c := models.Component{
Name: name, Title: box.Title, HTML: *box.HTML,
W: box.W, H: box.H, Framed: box.Framed, AdminOnly: box.AdminOnly,
W: box.W, H: box.H, Framed: box.Framed, Shadow: box.Shadow, AdminOnly: box.AdminOnly,
}
err = r.Store.DB.Transaction(func(tx *gorm.DB) error {
if err := tx.Create(&c).Error; err != nil {
+106 -4
View File
@@ -88,6 +88,7 @@ type ComplexityRoot struct {
ID func(childComplexity int) int
Kind func(childComplexity int) int
Page func(childComplexity int) int
Shadow func(childComplexity int) int
Title func(childComplexity int) int
UpdatedAt func(childComplexity int) int
W func(childComplexity int) int
@@ -105,6 +106,7 @@ type ComplexityRoot struct {
HTML func(childComplexity int) int
ID func(childComplexity int) int
Name func(childComplexity int) int
Shadow func(childComplexity int) int
Title func(childComplexity int) int
UpdatedAt func(childComplexity int) int
Uses func(childComplexity int) int
@@ -587,6 +589,12 @@ func (e *executableSchema) Complexity(ctx context.Context, typeName, field strin
}
return e.ComplexityRoot.Box.Page(childComplexity), true
case "Box.shadow":
if e.ComplexityRoot.Box.Shadow == nil {
break
}
return e.ComplexityRoot.Box.Shadow(childComplexity), true
case "Box.title":
if e.ComplexityRoot.Box.Title == nil {
break
@@ -672,6 +680,12 @@ func (e *executableSchema) Complexity(ctx context.Context, typeName, field strin
}
return e.ComplexityRoot.Component.Name(childComplexity), true
case "Component.shadow":
if e.ComplexityRoot.Component.Shadow == nil {
break
}
return e.ComplexityRoot.Component.Shadow(childComplexity), true
case "Component.title":
if e.ComplexityRoot.Component.Title == nil {
break
@@ -1966,6 +1980,8 @@ func (ec *executionContext) childFields_Box(ctx context.Context, field graphql.C
return ec.fieldContext_Box_z(ctx, field)
case "framed":
return ec.fieldContext_Box_framed(ctx, field)
case "shadow":
return ec.fieldContext_Box_shadow(ctx, field)
case "hideOnMobile":
return ec.fieldContext_Box_hideOnMobile(ctx, field)
case "adminOnly":
@@ -1996,6 +2012,8 @@ func (ec *executionContext) childFields_Component(ctx context.Context, field gra
return ec.fieldContext_Component_h(ctx, field)
case "framed":
return ec.fieldContext_Component_framed(ctx, field)
case "shadow":
return ec.fieldContext_Component_shadow(ctx, field)
case "adminOnly":
return ec.fieldContext_Component_adminOnly(ctx, field)
case "uses":
@@ -3848,6 +3866,29 @@ func (ec *executionContext) fieldContext_Box_framed(_ context.Context, field gra
return graphql.NewScalarFieldContext("Box", field, false, false, errors.New("field of type Boolean does not have child fields"))
}
func (ec *executionContext) _Box_shadow(ctx context.Context, field graphql.CollectedField, obj *models.Box) (ret graphql.Marshaler) {
return graphql.ResolveField(
ctx,
ec.OperationContext,
field,
func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) {
return ec.fieldContext_Box_shadow(ctx, field)
},
func(ctx context.Context) (any, error) {
return obj.Shadow, nil
},
nil,
func(ctx context.Context, selections ast.SelectionSet, v string) graphql.Marshaler {
return ec.marshalNString2string(ctx, selections, v)
},
true,
true,
)
}
func (ec *executionContext) fieldContext_Box_shadow(_ context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) {
return graphql.NewScalarFieldContext("Box", field, false, false, errors.New("field of type String does not have child fields"))
}
func (ec *executionContext) _Box_hideOnMobile(ctx context.Context, field graphql.CollectedField, obj *models.Box) (ret graphql.Marshaler) {
return graphql.ResolveField(
ctx,
@@ -4124,6 +4165,29 @@ func (ec *executionContext) fieldContext_Component_framed(_ context.Context, fie
return graphql.NewScalarFieldContext("Component", field, false, false, errors.New("field of type Boolean does not have child fields"))
}
func (ec *executionContext) _Component_shadow(ctx context.Context, field graphql.CollectedField, obj *models.Component) (ret graphql.Marshaler) {
return graphql.ResolveField(
ctx,
ec.OperationContext,
field,
func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) {
return ec.fieldContext_Component_shadow(ctx, field)
},
func(ctx context.Context) (any, error) {
return obj.Shadow, nil
},
nil,
func(ctx context.Context, selections ast.SelectionSet, v string) graphql.Marshaler {
return ec.marshalNString2string(ctx, selections, v)
},
true,
true,
)
}
func (ec *executionContext) fieldContext_Component_shadow(_ context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) {
return graphql.NewScalarFieldContext("Component", field, false, false, errors.New("field of type String does not have child fields"))
}
func (ec *executionContext) _Component_adminOnly(ctx context.Context, field graphql.CollectedField, obj *models.Component) (ret graphql.Marshaler) {
return graphql.ResolveField(
ctx,
@@ -10128,7 +10192,7 @@ func (ec *executionContext) unmarshalInputCreateBoxInput(ctx context.Context, ob
asMap[k] = v
}
fieldsInOrder := [...]string{"page", "kind", "widget", "html", "title", "x", "y", "w", "h", "z", "framed", "hideOnMobile", "adminOnly", "componentId"}
fieldsInOrder := [...]string{"page", "kind", "widget", "html", "title", "x", "y", "w", "h", "z", "framed", "shadow", "hideOnMobile", "adminOnly", "componentId"}
for _, k := range fieldsInOrder {
v, ok := asMap[k]
if !ok {
@@ -10212,6 +10276,13 @@ func (ec *executionContext) unmarshalInputCreateBoxInput(ctx context.Context, ob
return it, err
}
it.Framed = data
case "shadow":
ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("shadow"))
data, err := ec.unmarshalOString2ᚖstring(ctx, v)
if err != nil {
return it, err
}
it.Shadow = data
case "hideOnMobile":
ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("hideOnMobile"))
data, err := ec.unmarshalOBoolean2ᚖbool(ctx, v)
@@ -10249,7 +10320,7 @@ func (ec *executionContext) unmarshalInputCreateComponentInput(ctx context.Conte
asMap[k] = v
}
fieldsInOrder := [...]string{"name", "title", "html", "w", "h", "framed", "adminOnly"}
fieldsInOrder := [...]string{"name", "title", "html", "w", "h", "framed", "shadow", "adminOnly"}
for _, k := range fieldsInOrder {
v, ok := asMap[k]
if !ok {
@@ -10298,6 +10369,13 @@ func (ec *executionContext) unmarshalInputCreateComponentInput(ctx context.Conte
return it, err
}
it.Framed = data
case "shadow":
ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("shadow"))
data, err := ec.unmarshalOString2ᚖstring(ctx, v)
if err != nil {
return it, err
}
it.Shadow = data
case "adminOnly":
ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("adminOnly"))
data, err := ec.unmarshalOBoolean2ᚖbool(ctx, v)
@@ -10636,7 +10714,7 @@ func (ec *executionContext) unmarshalInputUpdateBoxInput(ctx context.Context, ob
asMap[k] = v
}
fieldsInOrder := [...]string{"widget", "html", "title", "x", "y", "w", "h", "z", "framed", "hideOnMobile", "adminOnly"}
fieldsInOrder := [...]string{"widget", "html", "title", "x", "y", "w", "h", "z", "framed", "shadow", "hideOnMobile", "adminOnly"}
for _, k := range fieldsInOrder {
v, ok := asMap[k]
if !ok {
@@ -10706,6 +10784,13 @@ func (ec *executionContext) unmarshalInputUpdateBoxInput(ctx context.Context, ob
return it, err
}
it.Framed = data
case "shadow":
ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("shadow"))
data, err := ec.unmarshalOString2ᚖstring(ctx, v)
if err != nil {
return it, err
}
it.Shadow = data
case "hideOnMobile":
ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("hideOnMobile"))
data, err := ec.unmarshalOBoolean2ᚖbool(ctx, v)
@@ -10736,7 +10821,7 @@ func (ec *executionContext) unmarshalInputUpdateComponentInput(ctx context.Conte
asMap[k] = v
}
fieldsInOrder := [...]string{"name", "title", "html", "w", "h", "framed", "adminOnly"}
fieldsInOrder := [...]string{"name", "title", "html", "w", "h", "framed", "shadow", "adminOnly"}
for _, k := range fieldsInOrder {
v, ok := asMap[k]
if !ok {
@@ -10785,6 +10870,13 @@ func (ec *executionContext) unmarshalInputUpdateComponentInput(ctx context.Conte
return it, err
}
it.Framed = data
case "shadow":
ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("shadow"))
data, err := ec.unmarshalOString2ᚖstring(ctx, v)
if err != nil {
return it, err
}
it.Shadow = data
case "adminOnly":
ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("adminOnly"))
data, err := ec.unmarshalOBoolean2ᚖbool(ctx, v)
@@ -11304,6 +11396,11 @@ func (ec *executionContext) _Box(ctx context.Context, sel ast.SelectionSet, obj
if out.Values[i] == graphql.Null {
out.Invalids++
}
case "shadow":
out.Values[i] = ec._Box_shadow(ctx, field, obj)
if out.Values[i] == graphql.Null {
out.Invalids++
}
case "hideOnMobile":
out.Values[i] = ec._Box_hideOnMobile(ctx, field, obj)
if out.Values[i] == graphql.Null {
@@ -11397,6 +11494,11 @@ func (ec *executionContext) _Component(ctx context.Context, sel ast.SelectionSet
if out.Values[i] == graphql.Null {
atomic.AddUint32(&out.Invalids, 1)
}
case "shadow":
out.Values[i] = ec._Component_shadow(ctx, field, obj)
if out.Values[i] == graphql.Null {
atomic.AddUint32(&out.Invalids, 1)
}
case "adminOnly":
out.Values[i] = ec._Component_adminOnly(ctx, field, obj)
if out.Values[i] == graphql.Null {
+3 -2
View File
@@ -31,6 +31,7 @@ type layoutBox struct {
H int `json:"h"`
Z int `json:"z"`
Framed bool `json:"framed"`
Shadow string `json:"shadow,omitempty"`
HideOnMobile bool `json:"hideOnMobile"`
AdminOnly bool `json:"adminOnly"`
// ComponentID is the component a linked box was linked to. HTML is
@@ -104,7 +105,7 @@ func snapshotPage(db *gorm.DB, page, name string) (layout models.Layout, empty b
stored[i] = layoutBox{
Kind: b.Kind, Widget: b.Widget, HTML: b.HTML, Title: b.Title,
X: b.X, Y: b.Y, W: b.W, H: b.H, Z: b.Z,
Framed: b.Framed, HideOnMobile: b.HideOnMobile, AdminOnly: b.AdminOnly,
Framed: b.Framed, Shadow: b.Shadow, HideOnMobile: b.HideOnMobile, AdminOnly: b.AdminOnly,
ComponentID: b.ComponentID,
}
}
@@ -217,7 +218,7 @@ func applyLayoutTo(tx *gorm.DB, layout *models.Layout, page string, replace bool
boxes[i] = models.Box{
Page: page, Kind: b.Kind, Widget: b.Widget, HTML: b.HTML, Title: b.Title,
X: b.X, Y: b.Y, W: b.W, H: b.H, Z: b.Z + zShift,
Framed: b.Framed, HideOnMobile: b.HideOnMobile, AdminOnly: b.AdminOnly,
Framed: b.Framed, Shadow: b.Shadow, HideOnMobile: b.HideOnMobile, AdminOnly: b.AdminOnly,
}
if b.ComponentID != nil && live[*b.ComponentID] {
boxes[i].ComponentID = b.ComponentID
+4
View File
@@ -42,6 +42,7 @@ type CreateBoxInput struct {
H *int `json:"h,omitempty"`
Z *int `json:"z,omitempty"`
Framed *bool `json:"framed,omitempty"`
Shadow *string `json:"shadow,omitempty"`
HideOnMobile *bool `json:"hideOnMobile,omitempty"`
AdminOnly *bool `json:"adminOnly,omitempty"`
ComponentID *int `json:"componentId,omitempty"`
@@ -54,6 +55,7 @@ type CreateComponentInput struct {
W *int `json:"w,omitempty"`
H *int `json:"h,omitempty"`
Framed *bool `json:"framed,omitempty"`
Shadow *string `json:"shadow,omitempty"`
AdminOnly *bool `json:"adminOnly,omitempty"`
}
@@ -167,6 +169,7 @@ type UpdateBoxInput struct {
H *int `json:"h,omitempty"`
Z *int `json:"z,omitempty"`
Framed *bool `json:"framed,omitempty"`
Shadow *string `json:"shadow,omitempty"`
HideOnMobile *bool `json:"hideOnMobile,omitempty"`
AdminOnly *bool `json:"adminOnly,omitempty"`
}
@@ -178,6 +181,7 @@ type UpdateComponentInput struct {
W *int `json:"w,omitempty"`
H *int `json:"h,omitempty"`
Framed *bool `json:"framed,omitempty"`
Shadow *string `json:"shadow,omitempty"`
AdminOnly *bool `json:"adminOnly,omitempty"`
}
+4
View File
@@ -24,6 +24,8 @@ type Box {
h: Int!
z: Int!
framed: Boolean!
# A CSS box-shadow value, or "" for none.
shadow: String!
hideOnMobile: Boolean!
adminOnly: Boolean!
componentId: ID
@@ -41,6 +43,7 @@ input CreateBoxInput {
h: Int
z: Int
framed: Boolean
shadow: String
hideOnMobile: Boolean
adminOnly: Boolean
# Links the new box to this component; kind is then "html" and html is
@@ -58,6 +61,7 @@ input UpdateBoxInput {
h: Int
z: Int
framed: Boolean
shadow: String
hideOnMobile: Boolean
adminOnly: Boolean
}
+5 -2
View File
@@ -2,7 +2,7 @@
# as a copy (an ordinary box) or linked (a Box whose componentId is set).
# Linked boxes share only the html — editing a component changes every box
# linked to it — while title, geometry, frame and visibility stay each box's
# own. title, w, h, framed and adminOnly here are what a newly placed box
# own. title, w, h, framed, shadow and adminOnly here are what a newly placed box
# starts with. See models.Component and graph/component_helpers.go.
#
# Admin only throughout, reads included, because a component may have been
@@ -19,6 +19,7 @@ type Component {
w: Int!
h: Int!
framed: Boolean!
shadow: String!
adminOnly: Boolean!
# How many boxes are linked to it.
uses: Int!
@@ -31,6 +32,7 @@ input CreateComponentInput {
w: Int
h: Int
framed: Boolean
shadow: String
adminOnly: Boolean
}
@@ -41,6 +43,7 @@ input UpdateComponentInput {
w: Int
h: Int
framed: Boolean
shadow: String
adminOnly: Boolean
}
@@ -57,7 +60,7 @@ extend type Mutation {
# no page changes.
deleteComponent(id: ID!): Component! @admin
# Makes html box `id` into a component named `name`, from its html, title,
# size, frame and adminOnly, and links the box to it. Answers with the box.
# size, frame, shadow and adminOnly, and links the box to it. Answers with the box.
saveBoxAsComponent(id: ID!, name: String!): Box! @admin
# Gives linked box `id` its own copy of the component's html and unlinks it.
detachBox(id: ID!): Box! @admin
+10 -5
View File
@@ -191,8 +191,11 @@ type Box struct {
// No gorm default on Framed: GORM omits zero values for defaulted columns
// on insert, so default:true would turn an explicit false into true.
// CreateBox applies the true default itself.
Framed bool `gorm:"not null" json:"framed"`
HideOnMobile bool `gorm:"not null;default:false" json:"hideOnMobile"`
Framed bool `gorm:"not null" json:"framed"`
// Shadow is a CSS box-shadow value drawn around the box, or "" for none.
// It is independent of Framed, so an unframed box can still cast one.
Shadow string `gorm:"not null;default:''" json:"shadow"`
HideOnMobile bool `gorm:"not null;default:false" json:"hideOnMobile"`
// AdminOnly boxes are never sent to anyone but an admin (the boxes
// resolver filters them out), so they suit private notes.
AdminOnly bool `gorm:"not null;default:false" json:"adminOnly"`
@@ -266,7 +269,8 @@ type Layout struct {
// boxes share only the HTML — editing it changes every one of them at once —
// while title, geometry, frame and visibility stay each box's own, since
// those are about where it sits on a page rather than what it is. Title, W,
// H, Framed and AdminOnly are the defaults a newly placed box starts with.
// H, Framed, Shadow and AdminOnly are the defaults a newly placed box starts
// with.
// Widgets are not components: a widget box is just a name, with nothing
// worth sharing. The unique index is partial for the same reason as Page's.
type Component struct {
@@ -280,6 +284,7 @@ type Component struct {
W int `gorm:"not null" json:"w"`
H int `gorm:"not null" json:"h"`
// No gorm default on Framed, for the same reason as Box.Framed.
Framed bool `gorm:"not null" json:"framed"`
AdminOnly bool `gorm:"not null;default:false" json:"adminOnly"`
Framed bool `gorm:"not null" json:"framed"`
Shadow string `gorm:"not null;default:''" json:"shadow"`
AdminOnly bool `gorm:"not null;default:false" json:"adminOnly"`
}