Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
generic component to highlight any part of the ui
- Loading branch information
Showing
9 changed files
with
217 additions
and
42 deletions.
There are no files selected for viewing
11 changes: 0 additions & 11 deletions
11
frontend/packages/console-app/src/components/tour/Spotlight.scss
This file was deleted.
Oops, something went wrong.
25 changes: 0 additions & 25 deletions
25
frontend/packages/console-app/src/components/tour/Spotlight.tsx
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 1 addition & 3 deletions
4
frontend/packages/console-app/src/components/tour/__tests__/TourStepComponent.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
frontend/packages/console-shared/src/components/spotlight/Spotlight.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
@keyframes expand { | ||
0% { | ||
outline-offset: -4px; | ||
outline-width: 4px; | ||
opacity: 1; | ||
} | ||
100% { | ||
outline-offset: 21px; | ||
outline-width: 12px; | ||
opacity: 0; | ||
} | ||
} | ||
|
||
@keyframes fade-in { | ||
0% { | ||
opacity: 0; | ||
} | ||
100% { | ||
opacity: 1; | ||
} | ||
} | ||
|
||
@keyframes fade-out { | ||
0% { | ||
opacity: 1; | ||
} | ||
100% { | ||
opacity: 0; | ||
} | ||
} | ||
|
||
.ocs-spotlight { | ||
pointer-events: none; | ||
position: absolute; | ||
&__with-backdrop { | ||
mix-blend-mode: hard-light; | ||
} | ||
&__element-highlight-noanimate { | ||
position: absolute; | ||
border: var(--pf-global--BorderWidth--xl) solid var(--pf-global--palette--blue-200); | ||
background-color: var(--pf-global--palette--black-500); | ||
z-index: 9998; | ||
} | ||
&__element-highlight-animate { | ||
pointer-events: none; | ||
position: absolute; | ||
box-shadow: inset 0px 0px 0px 4px var(--pf-global--palette--blue-200); | ||
opacity: 0; | ||
animation: 0.4s fade-in 0s ease-in-out, 5s fade-out 12.8s ease-in-out; | ||
animation-fill-mode: forwards; | ||
&::after { | ||
content: ''; | ||
position: absolute; | ||
left: 0; | ||
right: 0; | ||
top: 0; | ||
bottom: 0; | ||
animation: 1.2s expand 1.6s ease-out; | ||
animation-fill-mode: forwards; | ||
outline: 4px solid var(--pf-global--palette--blue-200); | ||
outline-offset: -4px; | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
frontend/packages/console-shared/src/components/spotlight/Spotlight.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import * as React from 'react'; | ||
import cx from 'classnames'; | ||
import Portal from '@console/shared/src/components/popper/Portal'; | ||
import './Spotlight.scss'; | ||
|
||
type SpotlightProps = { | ||
selector: string; | ||
animate?: boolean; | ||
hasBackdrop?: boolean; | ||
}; | ||
|
||
const isInViewport = (element) => { | ||
const rect = element.getBoundingClientRect(); | ||
return ( | ||
rect.top >= 0 && | ||
rect.left >= 0 && | ||
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && | ||
rect.right <= (window.innerWidth || document.documentElement.clientWidth) | ||
); | ||
}; | ||
|
||
const Spotlight: React.FC<SpotlightProps> = ({ selector, animate, hasBackdrop }) => { | ||
const element = document.querySelector(selector); | ||
const { height, width, top, left } = element.getBoundingClientRect(); | ||
const style: React.CSSProperties = { | ||
top, | ||
left, | ||
height, | ||
width, | ||
['--ocs-spotlight-top' as React.ReactText]: `${top}px`, | ||
['--ocs-spotlight-left' as React.ReactText]: `${left}px`, | ||
['--ocs-spotlight-height' as React.ReactText]: `${height}px`, | ||
['--ocs-spotlight-width' as React.ReactText]: `${width}px`, | ||
}; | ||
|
||
const [clicked, setClicked] = React.useState(false); | ||
React.useEffect(() => { | ||
if (!isInViewport(element)) { | ||
element.scrollIntoView(); | ||
} | ||
const handleClick = () => setClicked(true); | ||
document.addEventListener('mousedown', handleClick); | ||
return () => { | ||
document.removeEventListener('mousedown', handleClick); | ||
}; | ||
}, [clicked, element]); | ||
|
||
const highlight = ( | ||
<div | ||
className={cx('ocs-spotlight', { | ||
'ocs-spotlight__element-highlight-animate': animate && !clicked, | ||
'ocs-spotlight__element-highlight-noanimate': !animate, | ||
})} | ||
style={style} | ||
/> | ||
); | ||
return ( | ||
<Portal> | ||
{hasBackdrop ? ( | ||
<div className="pf-c-backdrop ocs-spotlight__with-backdrop">{highlight}</div> | ||
) : ( | ||
highlight | ||
)} | ||
</Portal> | ||
); | ||
}; | ||
|
||
export default Spotlight; |
80 changes: 80 additions & 0 deletions
80
frontend/packages/console-shared/src/components/spotlight/__tests__/Spotlight.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import * as React from 'react'; | ||
import { shallow, ShallowWrapper } from 'enzyme'; | ||
import Spotlight from '../Spotlight'; | ||
|
||
describe('Spotlight', () => { | ||
type SpotlightProps = React.ComponentProps<typeof Spotlight>; | ||
let wrapper: ShallowWrapper<SpotlightProps>; | ||
const uiElementPos = { height: 100, width: 100, top: 100, left: 100 }; | ||
const uiElement = { getBoundingClientRect: jest.fn().mockReturnValue(uiElementPos) }; | ||
beforeEach(() => { | ||
jest.spyOn(document, 'querySelector').mockImplementation(() => uiElement); | ||
}); | ||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
it('should render backdrop if hasBackdrop is set to true', () => { | ||
wrapper = shallow(<Spotlight selector="selector" hasBackdrop />); | ||
expect( | ||
wrapper | ||
.find('div') | ||
.first() | ||
.hasClass('pf-c-backdrop'), | ||
).toBe(true); | ||
expect( | ||
wrapper | ||
.find('div') | ||
.first() | ||
.hasClass('ocs-spotlight__with-backdrop'), | ||
).toBe(true); | ||
}); | ||
|
||
it('should not render backdrop if hasBackdrop is set to false', () => { | ||
wrapper = shallow(<Spotlight selector="selector" />); | ||
expect( | ||
wrapper | ||
.find('div') | ||
.first() | ||
.hasClass('pf-c-backdrop'), | ||
).toBe(false); | ||
expect( | ||
wrapper | ||
.find('div') | ||
.first() | ||
.hasClass('ocs-spotlight__with-backdrop'), | ||
).toBe(false); | ||
}); | ||
|
||
it('should render animation if animate is set to true', () => { | ||
wrapper = shallow(<Spotlight selector="selector" animate />); | ||
expect( | ||
wrapper | ||
.find('div') | ||
.first() | ||
.hasClass('ocs-spotlight__element-highlight-animate'), | ||
).toBe(true); | ||
expect( | ||
wrapper | ||
.find('div') | ||
.first() | ||
.hasClass('ocs-spotlight__element-highlight-noanimate'), | ||
).toBe(false); | ||
}); | ||
|
||
it('should not render animation if animate is set to false', () => { | ||
wrapper = shallow(<Spotlight selector="selector" />); | ||
expect( | ||
wrapper | ||
.find('div') | ||
.first() | ||
.hasClass('ocs-spotlight__element-highlight-animate'), | ||
).toBe(false); | ||
expect( | ||
wrapper | ||
.find('div') | ||
.first() | ||
.hasClass('ocs-spotlight__element-highlight-noanimate'), | ||
).toBe(true); | ||
}); | ||
}); |
1 change: 1 addition & 0 deletions
1
frontend/packages/console-shared/src/components/spotlight/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as Spotlight } from './Spotlight'; |