From c3f749e5817d842bd6c9e04faa2401a0905a26d3 Mon Sep 17 00:00:00 2001 From: Alison Joseph Date: Mon, 11 Nov 2024 13:54:30 -0600 Subject: [PATCH 1/2] fix(search): keyboard support for enter key --- .../components/GlobalSearch/GlobalSearch.js | 3 ++- .../src/components/GlobalSearch/Menu.js | 18 ++---------------- .../src/util/convertFilePathToUrl.js | 18 ++++++++++++++++++ yarn.lock | 4 ++-- 4 files changed, 24 insertions(+), 19 deletions(-) create mode 100644 packages/gatsby-theme-carbon/src/util/convertFilePathToUrl.js diff --git a/packages/gatsby-theme-carbon/src/components/GlobalSearch/GlobalSearch.js b/packages/gatsby-theme-carbon/src/components/GlobalSearch/GlobalSearch.js index db70361f2..c6f4f0a56 100644 --- a/packages/gatsby-theme-carbon/src/components/GlobalSearch/GlobalSearch.js +++ b/packages/gatsby-theme-carbon/src/components/GlobalSearch/GlobalSearch.js @@ -18,6 +18,7 @@ import { graphql, navigate, useStaticQuery } from 'gatsby'; import cx from 'classnames'; import NavContext from '../../util/context/NavContext'; import { useOnClickOutside } from '../../util/hooks'; +import { convertFilePathToUrl } from '../../util/convertFilePathToUrl'; import { container, @@ -118,7 +119,7 @@ const GlobalSearchInput = () => { case 'Enter': { e.preventDefault(); if (results[focusedItem]) { - navigate(results[focusedItem].path); + navigate(convertFilePathToUrl(results[focusedItem].path)); } break; } diff --git a/packages/gatsby-theme-carbon/src/components/GlobalSearch/Menu.js b/packages/gatsby-theme-carbon/src/components/GlobalSearch/Menu.js index 6172ccdf4..f5f440877 100644 --- a/packages/gatsby-theme-carbon/src/components/GlobalSearch/Menu.js +++ b/packages/gatsby-theme-carbon/src/components/GlobalSearch/Menu.js @@ -14,6 +14,8 @@ import { tab, } from './GlobalSearch.module.scss'; +import { convertFilePathToUrl } from '../../util/convertFilePathToUrl'; + export const MenuContext = createContext(); const Menu = ({ results, onKeyDown }) => { @@ -77,22 +79,6 @@ const MenuItem = ({ page, index, onKeyDown, id }) => { [active]: focusedItem === index, }); - function convertFilePathToUrl(filePath) { - const pagesIndex = filePath.lastIndexOf('/pages/'); - if (filePath.lastIndexOf('/pages/') === -1) return null; - - const fileName = filePath.slice( - pagesIndex + '/pages/'.length, - -'.mdx'.length - ); - const normalizedFileName = fileName.endsWith('/index') - ? fileName.slice(0, -'/index'.length) - : fileName; - const urlPath = `/${normalizedFileName.split('/').join('/')}/`; - - return urlPath; - } - const url = convertFilePathToUrl(page.path); return ( diff --git a/packages/gatsby-theme-carbon/src/util/convertFilePathToUrl.js b/packages/gatsby-theme-carbon/src/util/convertFilePathToUrl.js new file mode 100644 index 000000000..484ff85aa --- /dev/null +++ b/packages/gatsby-theme-carbon/src/util/convertFilePathToUrl.js @@ -0,0 +1,18 @@ +// utils/convertFilePathToUrl.js +export function convertFilePathToUrl(filePath) { + if (!filePath) return null; + + const pagesIndex = filePath.lastIndexOf('/pages/'); + if (pagesIndex === -1) return null; + + const fileName = filePath.slice( + pagesIndex + '/pages/'.length, + filePath.lastIndexOf('.') + ); + const normalizedFileName = fileName.endsWith('/index') + ? fileName.slice(0, -'/index'.length) + : fileName; + const urlPath = `/${normalizedFileName}/`; + + return urlPath; +} diff --git a/yarn.lock b/yarn.lock index ee05d1621..d61bba19b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -10135,7 +10135,7 @@ __metadata: dependencies: "@carbon/icons-react": "npm:^11.43.0" gatsby: "npm:^5.13.6" - gatsby-theme-carbon: "npm:^4.1.12" + gatsby-theme-carbon: "npm:^4.1.13" react: "npm:^18.3.1" react-dom: "npm:^18.3.1" languageName: unknown @@ -11436,7 +11436,7 @@ __metadata: languageName: unknown linkType: soft -"gatsby-theme-carbon@npm:^4.1.12, gatsby-theme-carbon@workspace:packages/gatsby-theme-carbon": +"gatsby-theme-carbon@npm:^4.1.13, gatsby-theme-carbon@workspace:packages/gatsby-theme-carbon": version: 0.0.0-use.local resolution: "gatsby-theme-carbon@workspace:packages/gatsby-theme-carbon" dependencies: From 6a5cb654718dafdc4e6a452c963a24463baab0ee Mon Sep 17 00:00:00 2001 From: Alison Joseph Date: Mon, 11 Nov 2024 13:55:22 -0600 Subject: [PATCH 2/2] chore: cleanup --- packages/gatsby-theme-carbon/src/util/convertFilePathToUrl.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/gatsby-theme-carbon/src/util/convertFilePathToUrl.js b/packages/gatsby-theme-carbon/src/util/convertFilePathToUrl.js index 484ff85aa..d97855896 100644 --- a/packages/gatsby-theme-carbon/src/util/convertFilePathToUrl.js +++ b/packages/gatsby-theme-carbon/src/util/convertFilePathToUrl.js @@ -1,4 +1,3 @@ -// utils/convertFilePathToUrl.js export function convertFilePathToUrl(filePath) { if (!filePath) return null;