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

Help Center: Align close button position #94026

Merged
merged 2 commits into from
Aug 29, 2024
Merged
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
20 changes: 18 additions & 2 deletions packages/help-center/src/hooks/use-opening-coordinates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@ import { useMobileBreakpoint } from '@automattic/viewport-react';
import { useState, useEffect } from 'react';

const AESTHETIC_OFFSET = 20;
const HELP_CENTER_POSITION_MASTERBAR = 11;
const HELP_CENTER_POSITION_EDITOR = 15;

const originElementOffset = ( element: HTMLElement ) => {
if ( element.classList.contains( 'masterbar__item' ) ) {
return HELP_CENTER_POSITION_MASTERBAR;
}
if ( element.classList.contains( 'entry-point-button' ) ) {
return HELP_CENTER_POSITION_EDITOR;
}
// opening it from another location, example `my home`
return 0;
};

/**
* This function calculates the position of the Help Center based on the last click event.
Expand Down Expand Up @@ -31,8 +44,10 @@ export const calculateOpeningPosition = ( element: HTMLElement ) => {

const { x, y, width, height } = element.getBoundingClientRect();

const buttonLeftEdge = x;
const buttonRightEdge = x + width;
const position = originElementOffset( element );

// handle RTL languages
const buttonLeftEdge = x - position;

const buttonTopEdge = y;
const buttonBottomEdge = y + height;
Expand All @@ -51,6 +66,7 @@ export const calculateOpeningPosition = ( element: HTMLElement ) => {

if ( buttonLeftEdge + helpCenterWidth + AESTHETIC_OFFSET > innerWidth ) {
// Align right edge of the help center with the right edge of the button
const buttonRightEdge = x + width + position;
coords.left = buttonRightEdge - helpCenterWidth;
coords.transformOrigin += ' right';
} else {
Expand Down
Loading