how to get the region (the menu is rendered in) or block name/ID in hook_preprocess_menu I have a menu that want to behave differently based on the region its displayed in.

By default there is no file suggestion that differentiates which twig file to be used for a menu based on the region.  Even if there was I think for the purpose of adding/removing a class it is more convinient to modify a variable in a preprocess function than keeping to versions of a twig template.

here is what I have 

function THEMENAME_preprocess_menu(&amp;$vars) {
    $test = "";
    if ($vars['menu_name'] == 'main') {
        if (!isset($vars['attributes']['class'])) {
            $vars['attributes']['class'] = [];
        }
        //test region or block name before merging
        $vars['attributes']['class'] = array_merge($vars['attributes']['class'], ['main-menu-regionA']);  
    }
}


I need a way to test the region or the block name to add a specific class to the parent UL element

my 2nd choice would be to explore the hook_theme_suggestions_alter
hook_preprocess_menu is missing a lot of variables that other preprocessors have so I finally solved my problem using hook_preprocess_block (which I think would not have been possible in earlier versions of drupal)

New solution since people have been reporting that $vars['configuration']['region'] is empty 

function THEMENAME_preprocess_block(&amp;$vars){
    if($vars['base_plugin_id'] == "system_menu_block"){
        if($vars['derivative_plugin_id'] == "main"){           
            $block = \Drupal\block\Entity\Block::load($vars["elements"]["#id"]);
            if( $block-&gt;getRegion() == "navbar")
                $vars['content']['#attributes']['class'][] = 'in-navbar';
                $vars['content']['#attributes']['class'][] = 'nav';
            }
            elseif($vars['configuration']['region'] == "footer"){
                $vars['content']['#attributes']['class'][] = 'in-footer';
            }
        }
    }
}


NOTE if using in a recursive function/twig MACRO (which probably you are) you should  removeClass all the classes you don't want in inner UL tags in a way similar to this 

{{ menus.menu_links(item.below, attributes.removeClass('nav','in-navbar'), menu_level + 1) }}


old solution

function THEMENAME_preprocess_block(&amp;$vars){
    if($vars['base_plugin_id'] == "system_menu_block"){
        if($vars['derivative_plugin_id'] == "main"){           
            if($vars['configuration']['region'] == "navbar"){
                $vars['content']['#attributes']['class'][] = 'in-navbar';
                $vars['content']['#attributes']['class'][] = 'nav';
            }
            elseif($vars['configuration']['region'] == "footer"){
                $vars['content']['#attributes']['class'][] = 'in-footer';
            }
        }
    }
}