Skip to content

box

The box field is a unique field that provides a tailored UX for defining values associated with the CSS box-model, such as margin, padding, and border-width.

Module field definition

javascript
padding: {
  label: 'Container padding',
  type: 'box',
  min: -3,
  max: 50,
  step: 2,
  def: {
    top: 20,
    right: -2,
    bottom: 0
    left: 30
  }
}

Box Field UI

The UI of the box field allows you to edit all values uniformly or each individually. Screenshot of the Apostrophe box field UI

Use in templates

The value of a box field is always an object with top, right, bottom, left properties. All property values are stored as numbers, any omitted values are made null. You can pull out each value in your template like

nunjucks
<button
  {% if data.piece.margin.left %}
    style="margin-left: {{ data.piece.margin.left }}px;"
  {% endif %}
>
  I might have a left margin
</button>

toCss helper function

There is also a helper function that will return a string of CSS rules.

ParameterTypeDefaultDescription
valueObjectn/aThe box field value
propertyStringn/aThe CSS property to assign values
unitString'px'The CSS unit
nunjucks
<button style="{{ apos.boxField.toCss( data.piece.margin, 'margin', 'em') }}">
  I might have margins
</button>

Settings

Required

PropertyTypeDefaultDescription
labelStringn/aSets the visible label for the field in the UI

Optional

PropertyTypeDefaultDescription
helpStringn/aHelp text for the content editor
defObject
{
 top: null,
 right: null,
 bottom: null,
 left: null
}
The default value. Must be an object with keys top, right, bottom, left. Each value must be a number
htmlHelpStringn/aHelp text with support for HTML markup
ifObject{}Conditions to meet before the field is active. See the guide for details.
requiredIfObject{}Conditions to meet before the field is required. See the guide for details.
hiddenBooleanfalseIf true, the field is hidden
maxNumbern/aThe maximum allowable value for any box property
minNumbern/aThe minimum allowable value for any box property
stepNumbern/aThe default increment when using the input's arrow buttons or keyboard
requiredBooleanfalseIf true, the field is mandatory
readOnlyBooleanfalseIf true, prevents the user from editing the field value