-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(DefinitionTooltip): add Definition Tooltip (#10262)
* feat(DefinitionTooltip): add definition tooltip * feat(DefinitionTooltip): add DefinitionTooltip styles * Update packages/styles/scss/components/tooltip/_tooltip.scss Co-authored-by: Josh Black <josh@josh.black> * fix(DefintionTooltip): fix blur trigger inconsitencies/adjust story copy * fix(Tooltip): format _tooltip.scss * Update packages/styles/scss/components/tooltip/_tooltip.scss Co-authored-by: Josh Black <josh@josh.black> * Update packages/styles/scss/components/tooltip/_tooltip.scss Co-authored-by: Josh Black <josh@josh.black> * fix(DefinitionTooltip): remove unneeded clickHandler * test(DefinitionTooltip): add DefinitionTooltip tests * test(DefinitionTooltip): add distinct DefinitionTooltip test file * feat(DefinitionTooltip): update underline styling, onClick bug * fix(DefinitionTooltip): fix font weight/color * feat(DefinitionTooltip): add openOnClick prop, update story * fix(TooltipDefinition): add on-hover styling * fix(react): update definition tooltip reveal behavior * fix(DefinitionTooltip): move spread props after className prop Co-authored-by: Josh Black <josh@josh.black>
- Loading branch information
Showing
5 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
packages/react/src/components/Tooltip/next/DefinitionTooltip.js
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,70 @@ | ||
/** | ||
* Copyright IBM Corp. 2016, 2018 | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import PropTypes from 'prop-types'; | ||
import React, { useState } from 'react'; | ||
import { Popover, PopoverContent } from '../../Popover'; | ||
import { match, keys } from '../../../internal/keyboard'; | ||
import { usePrefix } from '../../../internal/usePrefix'; | ||
import { useId } from '../../../internal/useId'; | ||
|
||
function DefinitionTooltip({ children, definition, ...rest }) { | ||
const [isOpen, setOpen] = useState(false); | ||
const prefix = usePrefix(); | ||
const id = useId(); | ||
|
||
function handleKeyDown(event) { | ||
if (isOpen && match(event, keys.Escape)) { | ||
event.stopPropagation(); | ||
setOpen(false); | ||
} | ||
} | ||
|
||
return ( | ||
<Popover | ||
align="bottom-left" | ||
dropShadow={false} | ||
highContrast | ||
onMouseLeave={() => { | ||
setOpen(false); | ||
}} | ||
open={isOpen}> | ||
<button | ||
className={`${prefix}--definition-term`} | ||
{...rest} | ||
aria-controls={id} | ||
aria-expanded={isOpen} | ||
onBlur={() => { | ||
setOpen(false); | ||
}} | ||
onClick={() => { | ||
setOpen(!isOpen); | ||
}} | ||
onKeyDown={handleKeyDown} | ||
type="button"> | ||
{children} | ||
</button> | ||
<PopoverContent className={`${prefix}--definition-tooltip`} id={id}> | ||
{definition} | ||
</PopoverContent> | ||
</Popover> | ||
); | ||
} | ||
|
||
DefinitionTooltip.propTypes = { | ||
/** | ||
* Provide the content being defined | ||
*/ | ||
children: PropTypes.node, | ||
|
||
/** | ||
* Provide the content being defined | ||
*/ | ||
definition: PropTypes.string.isRequired, | ||
}; | ||
|
||
export { DefinitionTooltip }; |
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
50 changes: 50 additions & 0 deletions
50
packages/react/src/components/Tooltip/next/__tests__/DefinitionTooltip-test.js
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,50 @@ | ||
/** | ||
* Copyright IBM Corp. 2016, 2018 | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import { render, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import React from 'react'; | ||
import { DefinitionTooltip } from '../../next/DefinitionTooltip'; | ||
|
||
describe('DefintiionTooltip', () => { | ||
it('should support a custom className with the `className` prop', () => { | ||
const definition = 'Uniform Resource Locator'; | ||
render( | ||
<DefinitionTooltip definition={definition} className="tooltip-class"> | ||
URL | ||
</DefinitionTooltip> | ||
); | ||
expect(screen.getByText('URL')).toHaveClass('tooltip-class'); | ||
}); | ||
|
||
it('should apply additional props to the outermost element', () => { | ||
const definition = 'Uniform Resource Locator'; | ||
render( | ||
<DefinitionTooltip | ||
data-testid="test" | ||
definition={definition} | ||
className="tooltip-class"> | ||
URL | ||
</DefinitionTooltip> | ||
); | ||
expect(screen.getByText('URL')).toHaveAttribute('data-testid', 'test'); | ||
}); | ||
|
||
it('should display onClick a defintion provided via prop', () => { | ||
const definition = 'Uniform Resource Locator'; | ||
render( | ||
<DefinitionTooltip | ||
data-testid="test" | ||
definition={definition} | ||
className="tooltip-class"> | ||
URL | ||
</DefinitionTooltip> | ||
); | ||
userEvent.click(screen.getByText('URL')); | ||
expect(screen.getByText(definition)).toHaveTextContent(definition); | ||
}); | ||
}); |
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 |
---|---|---|
|
@@ -10,3 +10,4 @@ | |
|
||
@include tooltip.tooltip; | ||
@include tooltip.icon-tooltip; | ||
@include tooltip.definition-tooltip; |
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