laravel-menu icon indicating copy to clipboard operation
laravel-menu copied to clipboard

Active link and item classes

Open ghost opened this issue 2 years ago • 4 comments

It would be great if it would be possible to specify the active class for each element, the tests showed everything ok works

Config:

'default' => array(
        'active_class_item' => 'open',
        'active_class_link' => 'active',
        'active_element' => 'item|link',  // item|link
    ),

Code: https://github.com/lavary/laravel-menu/blob/5255e715912b50127b296da7410bd97468bf5e40/src/Lavary/Menu/Item.php#L437

 if ('item' == $this->builder->conf('active_element')) {
            $item->active();
        } elseif('item|link' == $this->builder->conf('active_element')) {
            $item->active();
            $item->link->active();
        }else{
            $item->link->active();
        }

https://github.com/lavary/laravel-menu/blob/5255e715912b50127b296da7410bd97468bf5e40/src/Lavary/Menu/Item.php#L473

$this->attributes['class'] = Builder::formatGroupClass(array('class' => $this->builder->conf('active_class_item')), $this->attributes);

https://github.com/lavary/laravel-menu/blob/5255e715912b50127b296da7410bd97468bf5e40/src/Lavary/Menu/Link.php#L61

$this->attributes['class'] = Builder::formatGroupClass(array('class' => $this->builder ? $this->builder->conf('active_class_link') : null), $this->attributes);

It's a small thing but necessary

ghost avatar Mar 27 '22 23:03 ghost

Good idea. I wonder if it could be improved to fallback to active_class if the more granular active_class_link isn't set.

If someone wants to make this into a PR I would review.

dustingraham avatar May 19 '22 16:05 dustingraham

It's better this way:

'default' => array(
        'active_class_item' => 'open',
        'active_class_link' => 'active',
        'active_element' => ['item', 'link'],  // item, link
    ),

CODE:

       ...
       // Check to see which element should have class 'active' set.
        if (in_array('item', $this->builder->conf('active_element')))
            $item->active();
        
        if (in_array('link', $this->builder->conf('active_element')))
            $item->link->active();
       ...

ghost avatar May 19 '22 16:05 ghost

How can I contribute with your team

Tran-Quyen avatar Jun 16 '22 16:06 Tran-Quyen

A pull request is welcome.

dustingraham avatar Jun 19 '22 15:06 dustingraham