Class: Line::Message::Builder::Flex::Box

Inherits:
Base
  • Object
show all
Includes:
Actionable, HasPartial, Position::Margin, Position::Offset, Position::Padding, Size::Flex
Defined in:
lib/line/message/builder/flex/box.rb

Overview

Represents a "box" component in a LINE Flex Message.

A box is a fundamental layout container that arranges its child components (+contents+) in a specified direction (+layout+: horizontal, vertical, or baseline). It can hold various other components like text, images, buttons, or even nested boxes, allowing for complex layouts.

Boxes have numerous properties to control their appearance and the arrangement of their children, such as padding, margin, spacing between items, justification, and alignment.

A box can also have an action associated with it, making the entire box area tappable.

Example

Line::Message::Builder.with do
flex alt_text: "Box example" do
  bubble do
    body do
      layout :horizontal
      spacing :md
      text "Item 1"
      text "Item 2"
    end
  end
end
end

See also:

  • https://developers.line.biz/en/reference/messaging-api/#box
  • Actionable for making the box tappable
  • HasPartial for including reusable component groups
  • Position::Padding for padding properties
  • Position::Margin for margin properties
  • Position::Offset for offset properties
  • Size::Flex for flex sizing property

Instance Attribute Summary collapse

Attributes inherited from Base

#context

Instance Method Summary collapse

Methods included from Size::Flex

included

Methods included from Position::Offset

included

Methods included from Position::Margin

included

Methods included from Position::Padding

included

Methods included from Actionable

included, #message, #postback, #uri

Methods included from HasPartial

#partial!

Methods inherited from Base

inherited, #method_missing, #quick_reply, #respond_to_missing?

Constructor Details

#initialize(context: nil, **options) ⇒ Box

Initializes a new Flex Message Box component. The provided block is instance-eval'd, allowing DSL methods for adding child components (e.g., text, button, nested box) to be called.

[context] An optional context for the builder [options] A hash of options to set instance variables. Corresponds to the option definitions in this class and included modules [block] A block to define the contents of this box



273
274
275
276
# File 'lib/line/message/builder/flex/box.rb', line 273

def initialize(context: nil, **options, &)
  @contents = [] # Holds child components
  super # Calls Base#initialize, sets options, and evals block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Line::Message::Builder::Base

Instance Attribute Details

#contentsObject (readonly)

An array holding the child components (e.g., Text, Image, nested Box instances) of this box.



53
54
55
# File 'lib/line/message/builder/flex/box.rb', line 53

def contents
  @contents
end

Instance Method Details

#box(**options) ⇒ Object

Adds a nested Flex Box component to this box's contents.

[options] Options for the nested box (see Box) [block] A block to define the contents of the nested box

Returns the newly created nested Box object.



286
287
288
# File 'lib/line/message/builder/flex/box.rb', line 286

def box(**options, &)
  @contents << Flex::Box.new(context: context, **options, &)
end

#button(**options) ⇒ Object

Adds a Flex Button component to this box's contents.

[options] Options for the button component (see Button) [block] A block to define the button's action and properties

Returns the newly created Button object.



330
331
332
# File 'lib/line/message/builder/flex/box.rb', line 330

def button(**options, &)
  @contents << Flex::Button.new(context: context, **options, &)
end

#icon(url, **options) ⇒ Object

Adds an Icon component to this box's contents.

Icons decorate the text next to them and can only be used in a box whose layout is :baseline.

[url] The HTTPS URL of the icon image [options] Options for the icon component (e.g., :size, :aspect_ratio) [block] An optional block for further configuration

Example

box layout: :baseline do
icon "https://example.com/star.png", size: :sm
text "4.0", margin: :md
end


366
367
368
# File 'lib/line/message/builder/flex/box.rb', line 366

def icon(url, **options, &)
  @contents << Flex::Icon.new(url, context: context, **options, &)
end

#image(url, **options) ⇒ Object

Adds a Flex Image component to this box's contents.

[url] The URL of the image [options] Options for the image component (see Image) [block] An optional block for the image component (e.g., for an action)

Returns the newly created Image object.



344
345
346
# File 'lib/line/message/builder/flex/box.rb', line 344

def image(url, **options, &)
  @contents << Flex::Image.new(url, context: context, **options, &)
end

#separator(**options) ⇒ Object

Adds a Flex Separator component to this box's contents.

A separator is a simple component that draws a horizontal line, creating a visual division between other components in a container.

[options] Options for the separator component (see Separator) [block] An optional block for advanced separator configuration

Returns the newly created Separator object.



381
382
383
# File 'lib/line/message/builder/flex/box.rb', line 381

def separator(**options, &)
  @contents << Flex::Separator.new(context: context, **options, &)
end

#text(text = nil, **options) ⇒ Object

Adds a Flex Text component to this box's contents.

[text] The text content [options] Options for the text component (see Text) [block] An optional block for the text component. This can be used to define an action for the text or to add Span components within the text

Returns the newly created Text object.

Example: Simple text component

text "Hello, World!"

Example: Text with an action

text "Click me" do
message "You clicked me!", label: "Action"
end

Example: Text with spans

text "This has " do
span "styled", color: "#FF0000"
span " parts"
end


318
319
320
# File 'lib/line/message/builder/flex/box.rb', line 318

def text(text = nil, **options, &)
  @contents << Flex::Text.new(text, context: context, **options, &)
end

#to_hObject

:nodoc:

Raises:



385
386
387
388
389
390
391
# File 'lib/line/message/builder/flex/box.rb', line 385

def to_h # :nodoc:
  raise RequiredError, "layout is required" if layout.nil?

  return to_sdkv2 if context.sdkv2?

  to_api
end