1818 */
1919import { useState , useEffect } from 'react' ;
2020import { styled , css , useTheme } from '@apache-superset/core/ui' ;
21- import { debounce } from 'lodash' ;
2221import { getUrlParam } from 'src/utils/urlUtils' ;
23- import { MainNav , MenuMode } from '@superset-ui/core/components/Menu' ;
22+ import { MainNav , MenuItem } from '@superset-ui/core/components/Menu' ;
2423import { Tooltip , Grid , Row , Col , Image } from '@superset-ui/core/components' ;
2524import { GenericLink } from 'src/components' ;
2625import { NavLink , useLocation } from 'react-router-dom' ;
@@ -93,27 +92,7 @@ const StyledMainNav = styled(MainNav)`
9392 margin-inline-start : 0 ;
9493 }
9594
96- @media (max-width : 767px ) {
97- .ant-menu-item {
98- padding : 0 ${ theme . sizeUnit * 6 } px 0 ${ theme . sizeUnit * 3 } px !important ;
99- }
100-
101- .ant-menu > .ant-menu-item > span > a {
102- padding : 0 ;
103- }
104-
105- & .main-nav .ant-menu-submenu-title > svg : nth-of-type (1 ) {
106- display : none;
107- }
108- }
109- ` }
110- ` ;
111-
112- const { SubMenu } = MainNav ;
113-
114- const StyledSubMenu = styled ( SubMenu ) `
115- ${ ( { theme } ) => css `
116- & .ant-menu-submenu .ant-menu-submenu-horizontal {
95+ .ant-menu-submenu .ant-menu-submenu-horizontal {
11796 display : flex;
11897 align-items : center;
11998 height : 100% ;
@@ -156,7 +135,7 @@ const StyledSubMenu = styled(SubMenu)`
156135 }
157136 }
158137
159- & .ant-menu-submenu-selected .ant-menu-submenu-horizontal ::after {
138+ .ant-menu-submenu-selected .ant-menu-submenu-horizontal ::after {
160139 transform : scale (1 );
161140 }
162141 ` }
@@ -197,6 +176,10 @@ const StyledCol = styled(Col)`
197176 ` }
198177` ;
199178
179+ const StyledImage = styled ( Image ) `
180+ object-fit: contain;
181+ ` ;
182+
200183const { useBreakpoint } = Grid ;
201184
202185export function Menu ( {
@@ -209,23 +192,10 @@ export function Menu({
209192 } ,
210193 isFrontendRoute = ( ) => false ,
211194} : MenuProps ) {
212- const [ showMenu , setMenu ] = useState < MenuMode > ( 'horizontal' ) ;
213195 const screens = useBreakpoint ( ) ;
214196 const uiConfig = useUiConfig ( ) ;
215197 const theme = useTheme ( ) ;
216198
217- useEffect ( ( ) => {
218- function handleResize ( ) {
219- if ( window . innerWidth <= 767 ) {
220- setMenu ( 'inline' ) ;
221- } else setMenu ( 'horizontal' ) ;
222- }
223- handleResize ( ) ;
224- const windowResize = debounce ( ( ) => handleResize ( ) , 10 ) ;
225- window . addEventListener ( 'resize' , windowResize ) ;
226- return ( ) => window . removeEventListener ( 'resize' , windowResize ) ;
227- } , [ ] ) ;
228-
229199 enum Paths {
230200 Explore = '/explore' ,
231201 Dashboard = '/dashboard' ,
@@ -261,73 +231,63 @@ export function Menu({
261231 const standalone = getUrlParam ( URL_PARAMS . standalone ) ;
262232 if ( standalone || uiConfig . hideNav ) return < > </ > ;
263233
264- const renderSubMenu = ( {
234+ const buildMenuItem = ( {
265235 label,
266236 childs,
267237 url,
268- index,
269238 isFrontendRoute,
270- } : MenuObjectProps ) => {
239+ } : MenuObjectProps ) : MenuItem => {
271240 if ( url && isFrontendRoute ) {
272- return (
273- < MainNav . Item key = { label } role = "presentation" >
241+ return {
242+ key : label ,
243+ label : (
274244 < NavLink role = "button" to = { url } activeClassName = "is-active" >
275245 { label }
276246 </ NavLink >
277- </ MainNav . Item >
278- ) ;
247+ ) ,
248+ } ;
279249 }
250+
280251 if ( url ) {
281- return (
282- < MainNav . Item key = { label } >
283- < Typography . Link href = { url } > { label } </ Typography . Link >
284- </ MainNav . Item >
285- ) ;
252+ return {
253+ key : label ,
254+ label : < Typography . Link href = { url } > { label } </ Typography . Link > ,
255+ } ;
286256 }
287- return (
288- < StyledSubMenu
289- key = { label }
290- title = { label }
291- popupOffset = { [ 0 , - 8 ] }
292- icon = {
293- showMenu === 'inline' ? < > </ > : < Icons . DownOutlined iconSize = "xs" />
294- }
295- >
296- { childs ?. map ( ( child : MenuObjectChildProps | string , index1 : number ) => {
297- if ( typeof child === 'string' && child === '-' && label !== 'Data' ) {
298- return < MainNav . Divider key = { `$${ index1 } ` } /> ;
299- }
300- if ( typeof child !== 'string' ) {
301- return (
302- < MainNav . Item key = { `${ child . label } ` } >
303- { child . isFrontendRoute ? (
304- < NavLink
305- to = { child . url || '' }
306- exact
307- activeClassName = "is-active"
308- >
309- { child . label }
310- </ NavLink >
311- ) : (
312- < Typography . Link href = { child . url } >
313- { child . label }
314- </ Typography . Link >
315- ) }
316- </ MainNav . Item >
317- ) ;
318- }
319- return null ;
320- } ) }
321- </ StyledSubMenu >
322- ) ;
257+
258+ const childItems : MenuItem [ ] = [ ] ;
259+ childs ?. forEach ( ( child : MenuObjectChildProps | string , index1 : number ) => {
260+ if ( typeof child === 'string' && child === '-' && label !== 'Data' ) {
261+ childItems . push ( { type : 'divider' , key : `divider-${ index1 } ` } ) ;
262+ } else if ( typeof child !== 'string' ) {
263+ childItems . push ( {
264+ key : `${ child . label } ` ,
265+ label : child . isFrontendRoute ? (
266+ < NavLink to = { child . url || '' } exact activeClassName = "is-active" >
267+ { child . label }
268+ </ NavLink >
269+ ) : (
270+ < Typography . Link href = { child . url } > { child . label } </ Typography . Link >
271+ ) ,
272+ } ) ;
273+ }
274+ } ) ;
275+
276+ return {
277+ key : label ,
278+ label,
279+ icon : < Icons . DownOutlined iconSize = "xs" /> ,
280+ popupOffset : [ 0 , - 8 ] ,
281+ children : childItems ,
282+ } ;
323283 } ;
324284 const renderBrand = ( ) => {
325285 let link ;
326286 if ( theme . brandLogoUrl ) {
327287 link = (
328288 < StyledBrandWrapper margin = { theme . brandLogoMargin } >
329289 < StyledBrandLink href = { theme . brandLogoHref } >
330- < Image
290+ < StyledImage
331291 preview = { false }
332292 src = { theme . brandLogoUrl }
333293 alt = { theme . brandLogoAlt || 'Apache Superset' }
@@ -342,7 +302,7 @@ export function Menu({
342302 // Kept as is for backwards compatibility with the old theme system / superset_config.py
343303 link = (
344304 < GenericLink className = "navbar-brand" to = { brand . path } >
345- < Image preview = { false } src = { brand . icon } alt = { brand . alt } />
305+ < StyledImage preview = { false } src = { brand . icon } alt = { brand . alt } />
346306 </ GenericLink >
347307 ) ;
348308 } else {
@@ -352,7 +312,7 @@ export function Menu({
352312 href = { brand . path }
353313 tabIndex = { - 1 }
354314 >
355- < Image preview = { false } src = { brand . icon } alt = { brand . alt } />
315+ < StyledImage preview = { false } src = { brand . icon } alt = { brand . alt } />
356316 </ Typography . Link >
357317 ) ;
358318 }
@@ -377,15 +337,13 @@ export function Menu({
377337 </ StyledBrandText >
378338 ) }
379339 < StyledMainNav
380- mode = { showMenu }
340+ mode = "horizontal"
381341 data-test = "navbar-top"
382342 className = "main-nav"
383343 selectedKeys = { activeTabs }
384344 disabledOverflow
385- >
386- { menu . map ( ( item , index ) => {
345+ items = { menu . map ( item => {
387346 const props = {
388- index,
389347 ...item ,
390348 isFrontendRoute : isFrontendRoute ( item . url ) ,
391349 childs : item . childs ?. map ( c => {
@@ -400,9 +358,9 @@ export function Menu({
400358 } ) ,
401359 } ;
402360
403- return renderSubMenu ( props ) ;
361+ return buildMenuItem ( props ) ;
404362 } ) }
405- </ StyledMainNav >
363+ / >
406364 </ StyledCol >
407365 < Col md = { 8 } xs = { 24 } >
408366 < RightMenu
0 commit comments