diff --git a/packages/components/docs/sass.md b/packages/components/docs/sass.md index e8ddae4b7dcd..b8e6168c7802 100644 --- a/packages/components/docs/sass.md +++ b/packages/components/docs/sass.md @@ -3806,6 +3806,7 @@ $spacing-05: $carbon--spacing-05; - [search [mixin]](#search-mixin) - [select [mixin]](#select-mixin) - [tabs [mixin]](#tabs-mixin) + - [tooltip [mixin]](#tooltip-mixin) - [treeview [mixin]](#treeview-mixin) - [carbon-switcher [mixin]](#carbon-switcher-mixin) @@ -26985,7 +26986,6 @@ Tooltip styles } .#{$prefix}--tooltip { - @include box-shadow; @include reset; position: absolute; @@ -26997,7 +26997,6 @@ Tooltip styles padding: $carbon--spacing-05; color: $inverse-01; word-wrap: break-word; - background: $inverse-02; border-radius: rem(2px); // Windows, Firefox HCM Fix @@ -27046,6 +27045,11 @@ Tooltip styles } } + .#{$prefix}--tooltip__content { + background-color: $inverse-02; + box-shadow: 0 0 0 $spacing-05 $inverse-02, 0 2px 6px $spacing-05 rgba(0, 0, 0, 0.2); + } + // Tooltips need to be click focusable but not sequentially focusable so the user can click within // the tooltip and not have it close. Because the element is not actionable it does not need // to have a visible focus indicator (OK'd by IBMa) @@ -27067,6 +27071,44 @@ Tooltip styles content: ''; } + &.#{$prefix}--tooltip--top.#{$prefix}--tooltip--align-start, + &.#{$prefix}--tooltip--bottom.#{$prefix}--tooltip--align-start { + .#{$prefix}--tooltip__content { + transform: translate(50%, 0); + } + } + + &.#{$prefix}--tooltip--top.#{$prefix}--tooltip--align-end, + &.#{$prefix}--tooltip--bottom.#{$prefix}--tooltip--align-end { + .#{$prefix}--tooltip__content { + transform: translate(-50%, 0); + } + } + + &.#{$prefix}--tooltip--left.#{$prefix}--tooltip--align-start { + .#{$prefix}--tooltip__content { + transform: translate(0, calc(9px + 50% - #{$spacing-05})); + } + } + + &.#{$prefix}--tooltip--right.#{$prefix}--tooltip--align-start { + .#{$prefix}--tooltip__content { + transform: translate(0, calc(-3px + 50% - #{$spacing-05})); + } + } + + &.#{$prefix}--tooltip--left.#{$prefix}--tooltip--align-end { + .#{$prefix}--tooltip__content { + transform: translate(0, calc(9px - 50% + #{$spacing-05})); + } + } + + &.#{$prefix}--tooltip--right.#{$prefix}--tooltip--align-end { + .#{$prefix}--tooltip__content { + transform: translate(0, calc(-3px - 50% + #{$spacing-05})); + } + } + .#{$prefix}--tooltip__footer { display: flex; align-items: center; @@ -27288,6 +27330,7 @@ Tooltip styles - [carbon--spacing-07 [variable]](#carbon--spacing-07-variable) - [inverse-focus-ui [variable]](#inverse-focus-ui-variable) - [inverse-link [variable]](#inverse-link-variable) + - [spacing-05 [variable]](#spacing-05-variable) - [spacing-03 [variable]](#spacing-03-variable) - [interactive-04 [variable]](#interactive-04-variable) diff --git a/packages/components/src/components/tooltip/_tooltip.scss b/packages/components/src/components/tooltip/_tooltip.scss index 98fe76ca28d3..05d40e1c7ce5 100644 --- a/packages/components/src/components/tooltip/_tooltip.scss +++ b/packages/components/src/components/tooltip/_tooltip.scss @@ -368,7 +368,6 @@ } .#{$prefix}--tooltip { - @include box-shadow; @include reset; position: absolute; @@ -380,7 +379,6 @@ padding: $carbon--spacing-05; color: $inverse-01; word-wrap: break-word; - background: $inverse-02; border-radius: rem(2px); // Windows, Firefox HCM Fix @@ -429,6 +427,12 @@ } } + .#{$prefix}--tooltip__content { + background-color: $inverse-02; + box-shadow: 0 0 0 $spacing-05 $inverse-02, + 0 2px 6px $spacing-05 rgba(0, 0, 0, 0.2); + } + // Tooltips need to be click focusable but not sequentially focusable so the user can click within // the tooltip and not have it close. Because the element is not actionable it does not need // to have a visible focus indicator (OK'd by IBMa) @@ -450,6 +454,44 @@ content: ''; } + &.#{$prefix}--tooltip--top.#{$prefix}--tooltip--align-start, + &.#{$prefix}--tooltip--bottom.#{$prefix}--tooltip--align-start { + .#{$prefix}--tooltip__content { + transform: translate(calc(50% - 6px), 0); + } + } + + &.#{$prefix}--tooltip--top.#{$prefix}--tooltip--align-end, + &.#{$prefix}--tooltip--bottom.#{$prefix}--tooltip--align-end { + .#{$prefix}--tooltip__content { + transform: translate(calc(6px - 50%), 0); + } + } + + &.#{$prefix}--tooltip--left.#{$prefix}--tooltip--align-start { + .#{$prefix}--tooltip__content { + transform: translate(0, calc(9px + 50% - #{$spacing-03})); + } + } + + &.#{$prefix}--tooltip--right.#{$prefix}--tooltip--align-start { + .#{$prefix}--tooltip__content { + transform: translate(0, calc(-3px + 50% - #{$spacing-03})); + } + } + + &.#{$prefix}--tooltip--left.#{$prefix}--tooltip--align-end { + .#{$prefix}--tooltip__content { + transform: translate(0, calc(9px - 50% + #{$spacing-03})); + } + } + + &.#{$prefix}--tooltip--right.#{$prefix}--tooltip--align-end { + .#{$prefix}--tooltip__content { + transform: translate(0, calc(-3px - 50% + #{$spacing-03})); + } + } + .#{$prefix}--tooltip__footer { display: flex; align-items: center; diff --git a/packages/react/src/components/Tooltip/Tooltip-story.js b/packages/react/src/components/Tooltip/Tooltip-story.js index 6aaf0fcb67c5..8d08cf53489b 100644 --- a/packages/react/src/components/Tooltip/Tooltip-story.js +++ b/packages/react/src/components/Tooltip/Tooltip-story.js @@ -27,8 +27,15 @@ const directions = { 'Top (top)': 'top', 'Right (right)': 'right', }; +const alignments = { + 'Start (start)': 'start', + 'Center (center)': 'center', + 'End (end)': 'end', +}; + const props = { withIcon: () => ({ + align: select('Tooltip alignment (align)', alignments, 'center'), direction: select('Tooltip direction (direction)', directions, 'bottom'), triggerText: text('Trigger text (triggerText)', 'Tooltip label'), tabIndex: number('Tab index (tabIndex in )', 0), @@ -39,6 +46,7 @@ const props = { }), withoutIcon: () => ({ showIcon: false, + align: select('Tooltip alignment (align)', alignments, 'center'), direction: select('Tooltip direction (direction)', directions, 'bottom'), triggerText: text('Trigger text (triggerText)', 'Tooltip label'), tabIndex: number('Tab index (tabIndex in )', 0), @@ -49,6 +57,7 @@ const props = { }), customIcon: () => ({ showIcon: true, + align: select('Tooltip alignment (align)', alignments, 'center'), direction: select('Tooltip direction (direction)', directions, 'bottom'), triggerText: text('Trigger text (triggerText)', 'Tooltip label'), tabIndex: number('Tab index (tabIndex in )', 0), @@ -67,6 +76,7 @@ const props = { }), customIconOnly: () => ({ showIcon: true, + align: select('Tooltip alignment (align)', alignments, 'center'), direction: select('Tooltip direction (direction)', directions, 'bottom'), iconDescription: 'Helpful Information', tabIndex: number('Tab index (tabIndex in )', 0), diff --git a/packages/react/src/components/Tooltip/Tooltip-test.js b/packages/react/src/components/Tooltip/Tooltip-test.js index dc7b093d3c19..08d2fab9615b 100644 --- a/packages/react/src/components/Tooltip/Tooltip-test.js +++ b/packages/react/src/components/Tooltip/Tooltip-test.js @@ -87,7 +87,9 @@ describe('Tooltip', () => { .find('[data-floating-menu-direction]') .first() .prop('className') - ).toBe(`${prefix}--tooltip ${prefix}--tooltip--shown tooltip--class`); + ).toBe( + `${prefix}--tooltip ${prefix}--tooltip--shown ${prefix}--tooltip--bottom ${prefix}--tooltip--align-center tooltip--class` + ); }); it('sets the trigger class', () => { expect(label.prop('className')).toBe( diff --git a/packages/react/src/components/Tooltip/Tooltip.js b/packages/react/src/components/Tooltip/Tooltip.js index 0d4d0771f71e..579d32bd4250 100644 --- a/packages/react/src/components/Tooltip/Tooltip.js +++ b/packages/react/src/components/Tooltip/Tooltip.js @@ -87,6 +87,12 @@ class Tooltip extends Component { } static propTypes = { + /** + * Specify the alignment (to the trigger button) of the tooltip. + * Can be one of: start, center, or end. + */ + align: PropTypes.oneOf(['start', 'center', 'end']), + /** * Contents to put into the tooltip. */ @@ -207,6 +213,7 @@ class Tooltip extends Component { }; static defaultProps = { + align: 'center', direction: DIRECTION_BOTTOM, focusTrap: true, renderIcon: Information, @@ -405,6 +412,7 @@ class Tooltip extends Component { className, triggerClassName, direction, + align, focusTrap, triggerText, showIcon, @@ -422,7 +430,11 @@ class Tooltip extends Component { const tooltipClasses = classNames( `${prefix}--tooltip`, - { [`${prefix}--tooltip--shown`]: open }, + { + [`${prefix}--tooltip--shown`]: open, + [`${prefix}--tooltip--${direction}`]: direction, + [`${prefix}--tooltip--align-${align}`]: align, + }, className );