Patterns ultimately inherit from the base njpanderson\Braid\Base\Pattern class, which provides the following properties and methods.

Properties

contexts

Contexts, explained in detail on the Context data page, define the various views by which a pattern can be tested.

This property defines the names of those contexts for the purposes of menu generation:

1
2
3
4
5
6
    /**
     * Pattern contexts
     *
     * @var array
     */
    protected $contexts = [];

label

Patterns will show a label derived from their class name. You can override it with this property:

1
2
3
4
5
6
    /**
     * Pattern label as used in the menu.
     *
     * @var string
     */
    protected string $label = '';

viewName

The view name dictates which component view should be loaded when the pattern is accessed. By default, this is inferred from the namespace of your pattern. Use this property to change the view location if needed:

1
2
3
4
5
6
    /**
     * Name of the view, overrides view resolution.
     *
     * @var string
     */
    protected string $viewName = '';

icon

Each pattern defines a single icon for use within the menu. This can be customised here. Braid uses Heroicon’s “outline” group to define icons. For example, if you wanted to use the document-check icon, use that string exactly as the $icon value:

1
2
3
4
5
6
7
    /**
     * The icon of the pattern.
     *
     * @var string
     * @see https://heroicons.com/
     */
    protected string $icon = 'document';

Required Methods

The following methods are required to be defined on your pattern classes.

contextData

The contextData method on your pattern definition is called by Braid whenever the pattern is viewed. It will send a single string $context argument and expects an instance of either njpanderson\Braid\Contracts\PatternContext or Illuminate\Contracts\View\View to be returned:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public function contextData(string $context): PatternContext|View
{
    switch ($context) {
    case 'no-image':
        return $this->makeContext(
            attributes: [
                'title' => 'About us',
                'link' => '/about-us'
            ],
            slot: '<p>' . fake()->sentence(rand(6, 15)) . '</p>'
        );

    case 'no-link':
        return $this->makeContext(
            attributes: [
                'image' => 'https://picsum.photos/600/150',
                'title' => 'About us'
            ],
            slot: '<p>' . fake()->sentence(rand(6, 15)) . '</p>'
        );

    default:
        return $this->makeContext(
            attributes: [
                'image' => 'https://picsum.photos/600/150',
                'title' => 'About us',
                'link' => '/about-us'
            ],
            slot: '<p>' . fake()->sentence(rand(6, 15)) . '</p>'
        );
    }
}

arguments

argument type description
$context string The context name being accessed. Will be default if either the default context is requested, or no contexts exist (see contexts).

returns

  • njpanderson\Braid\Contracts\PatternContext
  • Illuminate\Contracts\View\View

Overridable methods

The following methods can be overridden within your pattern classes and are called by Braid throughout the pattern’s lifecycle.

getLabel

Returns the pattern’s label. A function alternative to defining the $label property.

returns

  • string - The label as displayed within Braid.
1
2
3
4
5
6
7
8
9
    /**
     * Get the pattern's label
     *
     * @return string
     */
    public function getLabel(): string
    {
        // return 'Label';
    }

getIcon

Returns the pattern’s icon name. A function alternative to defining the $icon property.

See $icon for information on icon references.

1
2
3
4
5
6
7
8
9
    /**
     * Return the pattern's icon.
     *
     * @return string
     */
    public function getIcon(): string
    {
        // return 'document';
    }

Callable methods

The following methods can be called from within your pattern classes.

makeContext

This method can be used to create an instance of njpanderson\Braid\Dictionaries\PatternContext which, other than View instances, is the expected return type from contextData calls in your patterns.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$this->makeContext(
    attributes: [
        'class' => 'bg-red-500',
        'foo' => ['bar', 'baz']
    ],
    slot: 'Consequat vulputate porttitor a senectus fermentum.',
    scopedSlots: [
        'slot1' => $this->makeScopedSlot(
            slot: 'Ut repellat nisi hic nobis beatae quis nobis.',
            attributes: [
                'class' => 'bg-yellow-300 min-w-[2000px]'
            ]
        ),
        'slot2' => $this->makeScopedSlot(
            // ...
        ),
        // ...
    ]
);

arguments

argument type description
$attributes array An array of key/value pairs defining attributes and their values. In the case of Laravel or Livewire components, these attributes are sent as html-like attributes to the component as it is called. In Laravel subviews, it is sent as the array data to that view.
$slot string or View (since v0.0.55) or null Slot data to send Laravel or Livewire components.
$scopedSlots ScopedSlot|string[] An array of scoped slot definitions or strings (in the case that a slot needs no attributes). See makeScopedSlot.

makeScopedSlot

Creates a single scoped slot for use with makeContext.

1
2
3
4
5
6
$this->makeScopedSlot(
    slot: 'Ut repellat nisi hic nobis beatae quis nobis.',
    attributes: [
        'class' => 'bg-yellow-300 min-w-[2000px]'
    ]
);

arguments

argument type description
$slot string or View (since v0.0.55) or null Scoped slot data to send Laravel or Livewire components.
$attributes array An array of key/value pairs defining attributes and their values.