Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: arrow breaking in AILabel popover #17982

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9385,6 +9385,9 @@ Map {
],
"type": "oneOf",
},
"alignmentAxisOffset": Object {
"type": "number",
},
"as": Object {
"type": "elementType",
},
Expand Down
8 changes: 7 additions & 1 deletion packages/react/src/components/AILabel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ export const AILabel = React.forwardRef<HTMLDivElement, AILabelProps>(
? `${aiText} - ${slugLabel || ariaLabel}`
: `${aiText} - ${aiTextLabel || textLabel}`;

const isSmallIcon = ['xs', '2xs', 'mini'].includes(size);

return (
<div className={aiLabelClasses} ref={ref} id={id}>
{revertActive ? (
Expand All @@ -205,7 +207,11 @@ export const AILabel = React.forwardRef<HTMLDivElement, AILabelProps>(
<Undo />
</IconButton>
) : (
<Toggletip align={align} autoAlign={autoAlign} {...rest}>
<Toggletip
align={align}
autoAlign={autoAlign}
alignmentAxisOffset={isSmallIcon ? -24 : 0}
{...rest}>
<ToggletipButton
className={aiLabelButtonClasses}
label={ariaLabelText}>
Expand Down
39 changes: 39 additions & 0 deletions packages/react/src/components/Popover/__tests__/Popover-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { render, screen } from '@testing-library/react';
import React from 'react';
import { Popover, PopoverContent } from '../../Popover';
import userEvent from '@testing-library/user-event';
import { waitForPosition } from '../../ListBox/test-helpers';

const prefix = 'cds';

Expand Down Expand Up @@ -70,6 +71,44 @@ describe('Popover', () => {
expect(screen.getByTestId('test').firstChild).toHaveClass('test');
});

it('should have default caret height', async () => {
render(
<Popover
open
align="bottom"
data-testid="test"
autoAlign
className="test ai-label">
<button type="button">Settings</button>
<PopoverContent className="test"></PopoverContent>
</Popover>
);

await waitForPosition();
const caretContainer =
screen.getByTestId('test').lastChild.lastChild.firstChild;
expect(caretContainer).toHaveStyle({ left: '0px', top: '-6px' });
});

it('should change caret height in case of ai-label', async () => {
render(
<Popover
open
align="bottom"
data-testid="test"
autoAlign
className="test ai-label">
<button type="button">Settings</button>
<PopoverContent className="test ai-label"></PopoverContent>
</Popover>
);

await waitForPosition();
const caretContainer =
screen.getByTestId('test').lastChild.lastChild.firstChild;
expect(caretContainer).toHaveStyle({ left: '0px', top: '-7px' });
});

it('should forward additional props on the outermost element', () => {
const { container } = render(
<PopoverContent id="test" data-testid="test">
Expand Down
16 changes: 13 additions & 3 deletions packages/react/src/components/Popover/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@
highContrast = false,
onRequestClose,
open,
alignmentAxisOffset,
...rest
}: PopoverProps<E>,
forwardRef: ForwardedRef<Element>
Expand Down Expand Up @@ -202,7 +203,10 @@
// needs to be placed 1px further outside the popover content. To do so,
// we look to see if any of the children has a className containing "slug"
const initialCaretHeight = React.Children.toArray(children).some((x) => {
return (x as any)?.props?.className?.includes('slug');
return (
(x as any)?.props?.className?.includes('slug') ||
(x as any)?.props?.className?.includes('ai-label')
);
})
? 7
: 6;
Expand Down Expand Up @@ -243,7 +247,6 @@
}
}
});

const { refs, floatingStyles, placement, middlewareData } = useFloating(
enableFloatingStyles
? {
Expand All @@ -257,7 +260,14 @@

// Middleware order matters, arrow should be last
middleware: [
offset(!isTabTip ? popoverDimensions?.current?.offset : 0),
offset(
!isTabTip
? {
alignmentAxis: alignmentAxisOffset,
mainAxis: popoverDimensions?.current?.offset,
}
: 0

Check warning on line 269 in packages/react/src/components/Popover/index.tsx

View check run for this annotation

Codecov / codecov/patch

packages/react/src/components/Popover/index.tsx#L269

Added line #L269 was not covered by tests
),
autoAlign &&
flip({
fallbackPlacements: align.includes('bottom')
Expand Down
29 changes: 18 additions & 11 deletions packages/react/src/components/Toggletip/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import { usePrefix } from '../../internal/usePrefix';
import { PolymorphicProps } from '../../types/common';

type ToggletipLabelProps<E extends ElementType> = {
as?: E | undefined;
as?: E;
children?: ReactNode;
className?: string | undefined;
className?: string;
};

/**
Expand Down Expand Up @@ -82,12 +82,13 @@ function useToggletip() {
}

interface ToggletipProps<E extends ElementType> {
align?: PopoverAlignment | undefined;
as?: E | undefined;
autoAlign?: boolean | undefined;
className?: string | undefined;
align?: PopoverAlignment;
alignmentAxisOffset?: number;
as?: E;
autoAlign?: boolean;
className?: string;
children?: ReactNode;
defaultOpen?: boolean | undefined;
defaultOpen?: boolean;
}

/**
Expand Down Expand Up @@ -223,6 +224,11 @@ Toggletip.propTypes = {
'right-start',
]),

/**
* Provide an offset value for alignment axis.
*/
alignmentAxisOffset: PropTypes.number,

/**
* Provide a custom element or component to render the top-level node for the
* component.
Expand Down Expand Up @@ -253,8 +259,8 @@ Toggletip.propTypes = {

interface ToggletipButtonBaseProps {
children?: ReactNode;
className?: string | undefined;
label?: string | undefined;
className?: string;
label?: string;
}

export type ToggleTipButtonProps<T extends React.ElementType> =
Expand All @@ -264,6 +270,7 @@ export type ToggleTipButtonProps<T extends React.ElementType> =
* `ToggletipButton` controls the visibility of the Toggletip through mouse
* clicks and keyboard interactions.
*/

export const ToggletipButton = React.forwardRef(function ToggletipButton<
T extends React.ElementType,
>(
Expand Down Expand Up @@ -323,7 +330,7 @@ ToggletipButton.displayName = 'ToggletipButton';

interface ToggletipContentProps {
children?: ReactNode;
className?: string | undefined;
className?: string;
}

/**
Expand Down Expand Up @@ -365,7 +372,7 @@ export { ToggletipContent };

interface ToggleTipActionsProps {
children?: ReactNode;
className?: string | undefined;
className?: string;
}

/**
Expand Down
Loading