Skip to content

Commit a68ef47

Browse files
authored
fix(MenuHeading): Hide shadow initially (#2326)
1 parent 2613d1a commit a68ef47

File tree

4 files changed

+24
-43
lines changed

4 files changed

+24
-43
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ jobs:
4141
- uses: c-hive/gha-yarn-cache@v1
4242
- run : yarn --silent
4343
- run: yarn test
44-
- uses: paambaati/[email protected]
45-
env:
46-
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
4744
env:
4845
CI: true
4946
build:

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

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,37 +25,18 @@
2525
*/
2626

2727
import { renderWithTheme } from '@looker/components-test-utils'
28-
import { act } from '@testing-library/react'
29-
import React, { RefObject } from 'react'
28+
import { screen } from '@testing-library/react'
29+
import React from 'react'
3030
import { useElementVisibility } from './MenuHeading.hooks'
3131

32-
interface TestProps {
33-
callback: (...args: any[]) => void
34-
testRef: RefObject<any>
35-
}
36-
37-
const TestHook = ({ callback, testRef }: TestProps) => {
38-
callback(testRef)
39-
return null
32+
const TestHook = () => {
33+
const [isVisible, ref] = useElementVisibility()
34+
return <div ref={ref}>{isVisible.toString()}</div>
4035
}
4136

4237
describe('MenuHeading Hooks', () => {
43-
let isVisible: boolean
44-
45-
const testRef = {
46-
current: <div />,
47-
}
48-
49-
/* eslint-disable react-hooks/rules-of-hooks */
50-
const cb = (ref: RefObject<any>) => {
51-
isVisible = useElementVisibility(ref)
52-
}
53-
/* eslint-enable react-hooks/rules-of-hooks */
54-
5538
it('it returns true as the default visibility state', () => {
56-
act(() => {
57-
renderWithTheme(<TestHook callback={cb} testRef={testRef} />)
58-
})
59-
expect(isVisible).toEqual(true)
39+
renderWithTheme(<TestHook />)
40+
expect(screen.getByText('true')).toBeVisible()
6041
})
6142
})

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

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

27-
import { RefObject, useEffect, useState } from 'react'
28-
29-
export const useElementVisibility = (ref: RefObject<HTMLElement>): boolean => {
27+
import { useEffect, useState } from 'react'
28+
import { useCallbackRef } from '../utils'
29+
30+
export const useElementVisibility = (): [
31+
boolean,
32+
(node: HTMLElement | null) => void
33+
] => {
34+
const [element, ref] = useCallbackRef()
3035
const [isVisible, setIsVisible] = useState(true)
3136

3237
useEffect(() => {
@@ -42,16 +47,15 @@ export const useElementVisibility = (ref: RefObject<HTMLElement>): boolean => {
4247
}
4348
)
4449

45-
const refCurrent = ref.current
46-
if (refCurrent && observer) {
47-
observer.observe && observer.observe(refCurrent)
50+
if (element && observer) {
51+
observer.observe && observer.observe(element)
4852
}
4953
return () => {
50-
if (refCurrent && observer) {
51-
observer.unobserve && observer.unobserve(refCurrent)
54+
if (element && observer) {
55+
observer.unobserve && observer.unobserve(element)
5256
}
5357
}
54-
}, [setIsVisible, ref])
58+
}, [element])
5559

56-
return isVisible
60+
return [isVisible, ref]
5761
}

packages/components/src/Menu/MenuHeading.tsx

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

27-
import React, { FC, useContext, useRef, ReactNode, RefObject } from 'react'
27+
import React, { FC, useContext, ReactNode } from 'react'
2828
import styled from 'styled-components'
2929
import {
3030
TextColorProps,
@@ -50,8 +50,7 @@ const MenuHeadingInternal: FC<MenuHeadingProps> = ({
5050
className,
5151
...restProps
5252
}) => {
53-
const labelShimRef: RefObject<any> = useRef()
54-
const isLabelShimVisible = useElementVisibility(labelShimRef)
53+
const [isLabelShimVisible, ref] = useElementVisibility()
5554

5655
const { density } = useContext(ListItemContext)
5756
const { px } = listItemDimensions(density)
@@ -67,7 +66,7 @@ const MenuHeadingInternal: FC<MenuHeadingProps> = ({
6766
we detect when this 0-height element disappears from the page and then
6867
render the shadow.
6968
*/}
70-
<div ref={labelShimRef} style={{ height: '0' }} />
69+
<div ref={ref} style={{ height: '0' }} />
7170
<Heading
7271
as="h2"
7372
color="text5"

0 commit comments

Comments
 (0)