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: add translations prop to search button #995

Merged
merged 2 commits into from Feb 5, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 14 additions & 7 deletions packages/docsearch-react/src/DocSearchButton.tsx
Expand Up @@ -3,10 +3,12 @@ import React, { useEffect, useState } from 'react';
import { ControlKeyIcon } from './icons/ControlKeyIcon';
import { SearchIcon } from './icons/SearchIcon';

export type DocSearchButtonProps = React.DetailedHTMLProps<
React.ButtonHTMLAttributes<HTMLButtonElement>,
HTMLButtonElement
>;
type Translations = Partial<{
buttonText: string;
buttonAriaLabel: string;
}>

export type DocSearchButtonProps = React.ComponentProps<'button'> & {translations?: Translations}

const ACTION_KEY_DEFAULT = 'Ctrl' as const;
const ACTION_KEY_APPLE = '⌘' as const;
Expand All @@ -18,7 +20,12 @@ function isAppleDevice() {
export const DocSearchButton = React.forwardRef<
HTMLButtonElement,
DocSearchButtonProps
>((props, ref) => {
>(({translations = {}, ...props}, ref) => {
const {
buttonText = "Search",
buttonAriaLabel = "Search"
} = translations;

const [key, setKey] = useState<
typeof ACTION_KEY_APPLE | typeof ACTION_KEY_DEFAULT | null
>(null);
Expand All @@ -33,13 +40,13 @@ export const DocSearchButton = React.forwardRef<
<button
type="button"
className="DocSearch DocSearch-Button"
aria-label="Search"
aria-label={buttonAriaLabel}
{...props}
ref={ref}
>
<span className="DocSearch-Button-Container">
<SearchIcon />
<span className="DocSearch-Button-Placeholder">Search</span>
<span className="DocSearch-Button-Placeholder">{buttonText}</span>
</span>

{key !== null ? (
Expand Down