Skip to content

Instantly share code, notes, and snippets.

@PatelUtkarsh
Last active October 20, 2021 05:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PatelUtkarsh/5fe03e0f53666824c911b9c5d9222a8f to your computer and use it in GitHub Desktop.
Save PatelUtkarsh/5fe03e0f53666824c911b9c5d9222a8f to your computer and use it in GitHub Desktop.
Material tab bar with icons
.site__navigation .tab-bar .mdc-tab .material-icons {
color: var(--mdc-theme-on-header, var(--mdc-theme-on-primary, #fff));
}
<?php
namespace child_theme\material;
/**
* Copied from MaterialDesign\Theme\Menu_Walker.
*
* Additional changes are in span.mdc-tab__content.
*/
class Custom_Walker extends Walker {
/**
* DB fields to use.
*
* @var array
*/
public $db_fields = [
'parent' => 'menu_item_parent',
'id' => 'db_id',
];
/**
* Add necessary classes to menu items
*
* @param string $output Used to append additional content (passed by reference).
* @param object $item The data object.
* @param int $depth Depth of the item.
* @param array $args An array of additional arguments.
* @param int $id ID of the current item.
*/
public function start_el( &$output, $item, $depth = 0, $args = [], $id = 0 ) {
$output .= sprintf(
'<a href="%1$s" %2$s>%3$s</a>',
esc_url( $item->url ),
( in_array( 'current-menu-item', $item->classes, true ) ) ? ' class="mdc-tab mdc-tab--active" aria-selected="true"' : ' class="mdc-tab"',
$this->build_markup( $item )
);
}
/**
* Add necessary markup to build tab
*
* @param object $item The data object.
* @return string Markup to display
*/
private function build_markup( $item ) {
$is_active = in_array( 'current-menu-item', $item->classes, true );
ob_start();
?>
<span class="mdc-tab__content">
<?php
echo wp_kses(
$item->title,
[
'span' => [
'class' => true,
'aria-hidden' => true,
],
]
);
?>
</span>
<span
class="mdc-tab-indicator
<?php
if ( $is_active ) {
echo 'mdc-tab-indicator--active';
}
?>
">
<span class="mdc-tab-indicator__content mdc-tab-indicator__content--underline"></span>
</span>
<span class="mdc-tab__ripple"></span>
<?php
return ob_get_clean();
}
}
<?php
namespace child_theme\material;
add_filter( 'wp_nav_menu_args', __NAMESPACE__ . '\\override_material_walker' );
function override_material_walker( $args ) {
require_once get_template_directory() . '/inc/custom_walker.php';
$args['walker'] = new Custom_Walker();
return $args;
}
<span class="mdc-tab__icon material-icons" aria-hidden="true">home</span><span class="mdc-tab__text-label">Home</span>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment