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: Add support for WP Support 3 tab component #96986

Merged
merged 4 commits into from
Dec 3, 2024
Merged
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
63 changes: 60 additions & 3 deletions packages/help-center/src/components/help-center-article.scss
Original file line number Diff line number Diff line change
Expand Up @@ -455,10 +455,67 @@
}
}

// Hide the tablist for the WP Support 3 tab component
// Context: https://github.com/Automattic/wp-calypso/issues/87340#issuecomment-1937424710
.wpsupport3-tab__tablist {
// Show the tab component for the WP Support 3 tab component
.wp-block-wpsupport3-tabs {

.aligncenter {
margin-left: auto;
margin-right: auto;
}

.wpsupport3-tab__tablist {
display: flex;
align-items: center;
gap: 24px;
white-space: nowrap;
overflow-x: auto;
border-bottom: 1px solid var( --wp--preset--color--border-light-gray, #eee );

.wpsupport3-tab__title {
padding: 8px 0;

color: var( --blue-blue-50, #0675c4 );
font-size: $font-body;
line-height: 24px;
border: none;
background: none;
border-bottom: 1px solid transparent;
transition: border-bottom 0.3s;

&:focus {
outline: none;
}

&:hover {
text-decoration: none;
}

&[aria-selected='true'] {
color: var( --gray-gray-100, #101517 );
border-bottom: 1px solid var( --gray-gray-100, #101517 );
}
}
}
}

.wp-block-wpsupport3-tab {
margin-top: 32px;
display: none;

&:not( [aria-hidden='true'] ) {
display: block;
animation: fadeIn 0.5s;
}

@keyframes fadeIn {
from {
opacity: 0;
}

to {
opacity: 1;
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { __ } from '@wordpress/i18n';
import { useSearchParams } from 'react-router-dom';
import { useHelpCenterContext } from '../contexts/HelpCenterContext';
import { usePostByUrl } from '../hooks';
import { useHelpCenterArticleTabComponent } from '../hooks/use-help-center-article-tab-component';
import { BackToTopButton } from './back-to-top-button';
import ArticleContent from './help-center-article-content';

Expand All @@ -12,11 +13,11 @@ import './help-center-article.scss';
export const HelpCenterArticle = () => {
const [ searchParams ] = useSearchParams();
const { sectionName } = useHelpCenterContext();

const postUrl = searchParams.get( 'link' ) || '';
const query = searchParams.get( 'query' );

const { data: post, isLoading, error } = usePostByUrl( postUrl );
useHelpCenterArticleTabComponent( post?.content );

useEffect( () => {
//If a url includes an anchor, let's scroll this into view!
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { useCallback, useEffect, useState } from '@wordpress/element';

export const useHelpCenterArticleTabComponent = ( postContent: string | undefined ) => {
const [ tabHash, setTabHash ] = useState( '' );

const toggleTab = ( element: Element, show: boolean ) => {
( element as HTMLElement ).style.display = show ? 'block' : 'none';
element.setAttribute( 'aria-hidden', show ? 'false' : 'true' );
};

const toggleTabTitle = ( element: Element, show: boolean ) => {
element.setAttribute( 'aria-selected', show ? 'true' : 'false' );
};

const activateTab = useCallback( () => {
const hash = tabHash;

const tabs = Array.from( document.querySelectorAll( '.wp-block-wpsupport3-tabs' ) );

tabs.forEach( ( tab ) => {
const titles = Array.from( tab.querySelectorAll( '.wpsupport3-tab__title' ) );
const bodies = Array.from(
tab.querySelectorAll( '.wp-block-wpsupport3-tab:not(.invisible_tabpanel)' )
);

const match = titles.findIndex( ( titles ) => titles.id === hash?.substring( 1 ) );

// Reset selection
titles.forEach( ( title ) => toggleTabTitle( title, false ) );
bodies.forEach( ( body ) => toggleTab( body, false ) );

if ( hash && match !== -1 ) {
toggleTabTitle( titles[ match ], true );
toggleTab( bodies[ match ], true );
} else {
// If the first tab is invisible from the editor, we set the first tab as active.
toggleTabTitle( titles[ 0 ], true );
toggleTab( bodies[ 0 ], true );
}
} );
}, [ tabHash ] );

useEffect( () => {
if ( tabHash || postContent ) {
activateTab();
}
}, [ activateTab, tabHash, postContent ] );

useEffect( () => {
if ( postContent ) {
const titles = Array.from(
document.querySelectorAll( '.wp-block-wpsupport3-tabs .wpsupport3-tab__title' )
);
titles.forEach( ( title ) => {
title.addEventListener( 'click', ( e ) => {
e.preventDefault();
setTabHash( `#${ title?.id }` );
setTimeout( () => {
window.scroll( 0, document.documentElement.scrollTop );
} );
} );
} );
}
}, [ postContent ] );
};
Loading