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

Allow setting menu defaults

Open timwhite opened this issue 6 years ago • 1 comments

It's great being able to set Link and List attributes on individual items, however it would be great if we could set defaults for them. The classes I need on the links and lists are different to you, and currently I need to set them on each menu item. Either being able to set defaults at render time, or at menu build time would be good. (And if you already can, improving the docs would be good)

        // Create Menu Items
        $menu->addChild('nav_config_general', 1)
            ->setLabel('Config General')
            ->setRoute('nav_config_general')
            ->setListAttr(['class' => 'nav-item'])
            ->setLinkAttr(['class' => 'nav-link'])
            // Contact
            ->addChildParent('nav_config_contact', 5)
            ->setLabel('Contact')
            ->setRoute('nav_config_contact')
            ->setListAttr(['class' => 'nav-item'])
            ->setLinkAttr(['class' => 'nav-link'])
            // Email
            ->addChildParent('nav_config_email', 10)
            ->setLabel('Email')
            ->setRoute('nav_config_email')
            ->setListAttr(['class' => 'nav-item'])
            ->setLinkAttr(['class' => 'nav-link'])
            ;

Could either become

        $menu->setDefaultListAttr(['class' => 'nav-item'])
            ->setDefaultLinkAttr(['class' => 'nav-link'])
        // Create Menu Items
        $menu->addChild('nav_config_general', 1)
            ->setLabel('Config General')
            ->setRoute('nav_config_general')
            // Contact
            ->addChildParent('nav_config_contact', 5)
            ->setLabel('Contact')
            ->setRoute('nav_config_contact')
            // Email
            ->addChildParent('nav_config_email', 10)
            ->setLabel('Email')
            ->setRoute('nav_config_email')
            ;

Or It could become

        // Create Menu Items
        $menu->addChild('nav_config_general', 1)
            ->setLabel('Config General')
            ->setRoute('nav_config_general')
            // Contact
            ->addChildParent('nav_config_contact', 5)
            ->setLabel('Contact')
            ->setRoute('nav_config_contact')
            // Email
            ->addChildParent('nav_config_email', 10)
            ->setLabel('Email')
            ->setRoute('nav_config_email')
            ;

and at render time

{{ pd_menu_render('App\\Menu\\FirstMenu', {
    'defaultLinkAttr': {'class': 'nav-link'},
    'defaultListAttr': {'class': 'nav-item'}
}) }}

timwhite avatar Jul 30 '18 20:07 timwhite

To further add to this. If Item was able to be extended, then we could set defaults. But all the "defaults" are private variables. If they are set to protected variables then they could be overridden in a class that extends Item and used to set the defaults.

timwhite avatar Aug 14 '19 09:08 timwhite