Skip to content

Commit

Permalink
feat: add align to tooltip
Browse files Browse the repository at this point in the history
  • Loading branch information
lee-chase committed Nov 18, 2020
1 parent 29e8535 commit ae0d27d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
48 changes: 46 additions & 2 deletions packages/components/src/components/tooltip/_tooltip.scss
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@
}

.#{$prefix}--tooltip {
@include box-shadow;
// @include box-shadow;
@include reset;

position: absolute;
Expand All @@ -380,7 +380,7 @@
padding: $carbon--spacing-05;
color: $inverse-01;
word-wrap: break-word;
background: $inverse-02;
// background: $inverse-02;
border-radius: rem(2px);

// Windows, Firefox HCM Fix
Expand Down Expand Up @@ -427,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)
Expand All @@ -448,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% - #{$spacing-05}), 0);
}
}

&.#{$prefix}--tooltip--top.#{$prefix}--tooltip--align-end,
&.#{$prefix}--tooltip--bottom.#{$prefix}--tooltip--align-end {
.#{$prefix}--tooltip__content {
transform: translate(calc(-50% + #{$spacing-05}), 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;
Expand Down
10 changes: 10 additions & 0 deletions packages/react/src/components/Tooltip/Tooltip-story.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,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 <Tooltip>)', 0),
Expand All @@ -38,6 +45,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 <Tooltip>)', 0),
Expand All @@ -48,6 +56,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 <Tooltip>)', 0),
Expand All @@ -66,6 +75,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 <Tooltip>)', 0),
Expand Down
14 changes: 13 additions & 1 deletion packages/react/src/components/Tooltip/Tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -207,6 +213,7 @@ class Tooltip extends Component {
};

static defaultProps = {
align: 'center',
direction: DIRECTION_BOTTOM,
focusTrap: true,
renderIcon: Information,
Expand Down Expand Up @@ -405,6 +412,7 @@ class Tooltip extends Component {
className,
triggerClassName,
direction,
align,
focusTrap,
triggerText,
showIcon,
Expand All @@ -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
);

Expand Down

0 comments on commit ae0d27d

Please sign in to comment.