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

feat(coachmark): Add default opening for not-stacked coachmarks #6516

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ const renderCoachmark = ({ ...rest } = {}, children = childrenContent) =>
</Coachmark>
);

const isCoachmarkVisible = () => {
const coachmarkContainer = screen.getByTestId(dataTestId);
const coachmarkButton = coachmarkContainer.getElementsByTagName('button')[0];
const ariaExpanded = coachmarkButton.getAttribute('aria-expanded');
return ariaExpanded === 'true';
};

describe(componentName, () => {
it('renders a component Coachmark', () => {
renderCoachmark({ 'data-testid': dataTestId });
Expand Down Expand Up @@ -203,4 +210,12 @@ describe(componentName, () => {
`${pkg.prefix}--coachmark__dark`
);
});

it('Check coachmark can be open by default', () => {
renderCoachmark({
'data-testid': dataTestId,
isOpenByDefault: true,
});
expect(isCoachmarkVisible()).toBeTruthy();
});
});
16 changes: 14 additions & 2 deletions packages/ibm-products/src/components/Coachmark/Coachmark.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const defaults = {
onClose: () => {},
overlayKind: 'tooltip',
theme: 'light',
isOpenByDefault: false,
};

export interface CoachmarkProps {
Expand Down Expand Up @@ -113,6 +114,11 @@ export interface CoachmarkProps {
* Determines the theme of the component.
*/
theme?: 'light' | 'dark';
/**
* Determines if the coachmark is open by default.
* Does nothing if `overlayKind=stacked`.
*/
isOpenByDefault?: boolean;
}

/**
Expand All @@ -136,15 +142,15 @@ export let Coachmark = forwardRef<HTMLElement, CoachmarkProps>(
portalTarget,
target,
theme = defaults.theme,

isOpenByDefault = defaults.isOpenByDefault,
// Collect any other property values passed in.
...rest
},
ref
) => {
const isBeacon = overlayKind === COACHMARK_OVERLAY_KIND.TOOLTIP;
const isStacked = overlayKind === COACHMARK_OVERLAY_KIND.STACKED;
const [isOpen, setIsOpen] = useState(isStacked);
const [isOpen, setIsOpen] = useState(isStacked || isOpenByDefault);
const [shouldResetPosition, setShouldResetPosition] = useState(false);
const [targetRect, setTargetRect] = useState();
const [targetOffset, setTargetOffset] = useState({ x: 0, y: 0 });
Expand Down Expand Up @@ -396,6 +402,12 @@ Coachmark.propTypes = {
*/
className: PropTypes.string,

/**
* Determines if the coachmark is open by default.
* Does nothing if `overlayKind=stacked`.
*/
isOpenByDefault: PropTypes.bool,

/**
* Function to call when the Coachmark closes.
*/
Expand Down
Loading