Skip to content

Commit 80c587c

Browse files
Menu group shadow fix (#33)
1 parent 4121d78 commit 80c587c

File tree

7 files changed

+114
-100
lines changed

7 files changed

+114
-100
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## UNRELEASED
99

1010
### Changed
11+
12+
- Fix bug where menu group headers render a drop shadow by default
13+
- Refactor z-index out of menu styles
1114
- Improvements to documentation layout and scrolling behavior
1215
- SVG for `ApplicationSelect`,`ArrowChange`,`Beaker`,`BrowseTable`,`ChangeHistory`,`DimensionFill`,`Explore`,`LogoRings`,`NoteOutline`,`Notes`,`Reports`,`SqlRunner`,`User`,`UserAttributes`,`Users`,`ViewGrid`,`VisArea`,`VisBar`,`VisColumn`,`VisLine`,`VisMap`,`VisPie`,`VisScatter`,`VisSingleValue`,`VisTable` icons resized redrawn to fit on the correct icon grid.
1316
- Implement a vertical layout for the Code Sandbox toolbar to prevent unintentional obfuscation of code samples

packages/components/src/Menu/MenuGroup.hooks.test.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
MIT License
44
55
Copyright (c) 2019 Looker Data Sciences, Inc.
6-
6+
77
Permission is hereby granted, free of charge, to any person obtaining a copy
88
of this software and associated documentation files (the "Software"), to deal
99
in the Software without restriction, including without limitation the rights
1010
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1111
copies of the Software, and to permit persons to whom the Software is
1212
furnished to do so, subject to the following conditions:
13-
13+
1414
The above copyright notice and this permission notice shall be included in all
1515
copies or substantial portions of the Software.
16-
16+
1717
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1818
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1919
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -52,10 +52,10 @@ describe('MenuGroup Hooks', () => {
5252
}
5353
/* eslint-enable react-hooks/rules-of-hooks */
5454

55-
it('it returns false as the default visibility state', () => {
55+
it('it returns true as the default visibility state', () => {
5656
act(() => {
5757
mount(<TestHook callback={cb} testRef={testRef} />)
5858
})
59-
expect(isVisible).toEqual(false)
59+
expect(isVisible).toEqual(true)
6060
})
6161
})

packages/components/src/Menu/MenuGroup.hooks.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import { RefObject, useEffect, useState } from 'react'
2828

2929
export const useElementVisibility = (ref: RefObject<HTMLElement>): boolean => {
30-
const [isVisible, setIsVisible] = useState(false)
30+
const [isVisible, setIsVisible] = useState(true)
3131

3232
const observer =
3333
typeof IntersectionObserver === 'undefined'
@@ -40,6 +40,7 @@ export const useElementVisibility = (ref: RefObject<HTMLElement>): boolean => {
4040
threshold: [0, 1],
4141
}
4242
)
43+
4344
useEffect(() => {
4445
const refCurrent = ref.current
4546
if (refCurrent) {

packages/components/src/Menu/MenuGroup.tsx

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,20 @@
2424
2525
*/
2626

27-
import React, { CSSProperties, FC, ReactNode, RefObject, useRef } from 'react'
27+
import React, { CSSProperties, FC, ReactNode } from 'react'
2828
import styled from 'styled-components'
2929
import {
3030
color,
31-
palette,
3231
CompatibleHTMLProps,
3332
reset,
3433
SpaceProps,
3534
space,
3635
} from '@looker/design-tokens'
3736
import { BackgroundColorProps } from 'styled-system'
38-
import { Heading, HeadingProps } from '../Text/Heading'
37+
import { HeadingProps } from '../Text/Heading'
3938
import { List } from '../List'
4039

4140
import { MenuGroupLabel } from './MenuGroupLabel'
42-
import { useElementVisibility } from './MenuGroup.hooks'
4341
import { MenuItemCustomization } from './MenuItem'
4442
import { cloneMenuListChildren } from './MenuList'
4543

@@ -67,55 +65,31 @@ const MenuGroupInternal: FC<MenuGroupWithChildrenProps> = ({
6765
}) => {
6866
const { customizationProps, compact, ...boxProps } = props
6967

70-
const labelShimRef: RefObject<any> = useRef()
71-
const labelVisible = useElementVisibility(labelShimRef)
72-
73-
const labelComponent = label && (
74-
<MenuGroupLabel
75-
backgroundColor={customizationProps && customizationProps.bg}
76-
boxShadow={
77-
labelVisible ? 'none' : `0 4px 8px -2px ${palette.charcoal200}`
78-
}
79-
>
80-
{/*
81-
NOTE: This div is required for box-shadow to appear when the heading
82-
is sticky to the top of the container. Using IntersectionObserver,
83-
we detect when this 0-height element disappears from the page and then
84-
render the shadow.
85-
*/}
86-
<div ref={labelShimRef} style={{ height: '0' }} />
87-
<Heading
88-
fontSize="small"
89-
as="h2"
90-
px="medium"
91-
py="xsmall"
92-
fontWeight="semiBold"
93-
{...labelProps}
94-
style={{ zIndex: 2, ...labelStyles }}
95-
>
96-
{label}
97-
</Heading>
98-
</MenuGroupLabel>
99-
)
100-
10168
const clonedChildren = cloneMenuListChildren(children as JSX.Element[], {
10269
compact,
10370
customizationProps,
10471
})
10572

10673
return (
107-
<Style
74+
<MenuGroupWrapper
10875
{...boxProps}
10976
backgroundColor={customizationProps && customizationProps.bg}
11077
py="small"
11178
>
112-
{labelComponent}
79+
{label && (
80+
<MenuGroupLabel
81+
backgroundColor={customizationProps && customizationProps.bg}
82+
labelStyles={labelStyles}
83+
labelContent={label}
84+
{...labelProps}
85+
/>
86+
)}
11387
<List nomarker>{clonedChildren}</List>
114-
</Style>
88+
</MenuGroupWrapper>
11589
)
11690
}
11791

118-
const Style = styled.li<MenuGroupProps>`
92+
const MenuGroupWrapper = styled.li<MenuGroupProps>`
11993
${reset}
12094
${space}
12195
${color}

packages/components/src/Menu/MenuGroupLabel.tsx

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,62 @@
2424
2525
*/
2626

27+
import React, { FC, useRef, RefObject, CSSProperties, ReactNode } from 'react'
2728
import styled from 'styled-components'
28-
import { color, BackgroundColorProps, BoxShadowProps } from 'styled-system'
29+
import { color, BackgroundColorProps, BorderRadiusProps } from 'styled-system'
30+
import { Heading, HeadingProps } from '../Text/Heading'
31+
import { useElementVisibility } from './MenuGroup.hooks'
2932

30-
interface MenuGroupLabelProps extends BackgroundColorProps, BoxShadowProps {}
33+
interface MenuGroupLabelProps
34+
extends BackgroundColorProps,
35+
HeadingProps,
36+
BorderRadiusProps {
37+
labelContent: ReactNode
38+
labelStyles?: CSSProperties
39+
}
40+
41+
export const MenuGroupLabel: FC<MenuGroupLabelProps> = ({
42+
labelContent,
43+
labelStyles,
44+
...props
45+
}) => {
46+
const labelShimRef: RefObject<any> = useRef()
47+
const isLabelShimVisible = useElementVisibility(labelShimRef)
48+
49+
return (
50+
<MenuGroupLabelWrapper renderBoxShadow={!isLabelShimVisible}>
51+
{/*
52+
NOTE: The labelShimRef div is required for box-shadow to appear when the heading
53+
is sticky to the top of the container. Using IntersectionObserver,
54+
we detect when this 0-height element disappears from the page and then
55+
render the shadow.
56+
*/}
57+
<div ref={labelShimRef} style={{ height: '0' }} />
58+
<Heading
59+
fontSize="small"
60+
as="h2"
61+
px="medium"
62+
py="xsmall"
63+
fontWeight="semiBold"
64+
style={labelStyles}
65+
{...props}
66+
>
67+
{labelContent}
68+
</Heading>
69+
</MenuGroupLabelWrapper>
70+
)
71+
}
72+
73+
interface MenuGroupLabelWrapperProps {
74+
renderBoxShadow: boolean
75+
}
3176

32-
export const MenuGroupLabel = styled.div<MenuGroupLabelProps>`
77+
const MenuGroupLabelWrapper = styled.div<MenuGroupLabelWrapperProps>`
3378
${color}
34-
box-shadow: ${props => props.boxShadow};
79+
box-shadow: ${({ renderBoxShadow, theme }) =>
80+
renderBoxShadow
81+
? `0 4px 8px -2px ${theme.colors.palette.charcoal200}`
82+
: 'none'};
3583
position: sticky;
3684
top: -1px;
3785
margin-bottom: ${({ theme }) => theme.space.xxsmall};

0 commit comments

Comments
 (0)