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

[graphiql v4] Allow reassign tab name #3753

Draft
wants to merge 2 commits into
base: graphiql-v4
Choose a base branch
from
Draft
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
5 changes: 5 additions & 0 deletions .changeset/lovely-bobcats-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'graphiql': minor
---

allow reassign name of the tabs
24 changes: 23 additions & 1 deletion packages/graphiql-react/src/ui/tabs.css
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,34 @@
min-width: 0;
}

.graphiql-tab-input {
all: unset;
width: 100%;
padding: var(--px-4) 28px var(--px-4) var(--px-8);
border-radius: var(--border-radius-8) var(--border-radius-8) 0 0;

&:focus {
outline: hsla(var(--color-neutral), var(--alpha-background-heavy)) auto 1px;
}

&[readonly] {
cursor: pointer;
/* remove selection when focusing */
&::selection {
user-select: none;
}
}
}

.graphiql-tab:has(input[readonly]) {
max-width: 140px;
}

.graphiql-tab {
border-radius: var(--border-radius-8) var(--border-radius-8) 0 0;
background: hsla(var(--color-neutral), var(--alpha-background-light));
position: relative;
display: flex;
max-width: 140px;

/* disable shrinking while changing the operation name */
&:not(:focus-within) {
Expand Down
51 changes: 46 additions & 5 deletions packages/graphiql/src/components/GraphiQL.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
Children,
JSX,
cloneElement,
MouseEvent,
FocusEvent,
ComponentProps,
} from 'react';

import {
Expand Down Expand Up @@ -240,6 +243,33 @@

const TAB_CLASS_PREFIX = 'graphiql-session-tab-';

const handleTabValueChange: ComponentProps<'input'>['onChange'] = event => {
const input = event.target;

Check warning on line 247 in packages/graphiql/src/components/GraphiQL.tsx

View check run for this annotation

Codecov / codecov/patch

packages/graphiql/src/components/GraphiQL.tsx#L247

Added line #L247 was not covered by tests
// Should be at least 1 character wide, otherwise will throw DOMException
input.size = Math.max(1, input.value.length - 1);

Check warning on line 249 in packages/graphiql/src/components/GraphiQL.tsx

View check run for this annotation

Codecov / codecov/patch

packages/graphiql/src/components/GraphiQL.tsx#L249

Added line #L249 was not covered by tests
};

const handleTabValueKeyDown: ComponentProps<'input'>['onKeyDown'] = event => {
if (event.key !== 'Enter' && event.key !== 'Escape') {
return;

Check warning on line 254 in packages/graphiql/src/components/GraphiQL.tsx

View check run for this annotation

Codecov / codecov/patch

packages/graphiql/src/components/GraphiQL.tsx#L254

Added line #L254 was not covered by tests
}
const input = event.currentTarget;

Check warning on line 256 in packages/graphiql/src/components/GraphiQL.tsx

View check run for this annotation

Codecov / codecov/patch

packages/graphiql/src/components/GraphiQL.tsx#L256

Added line #L256 was not covered by tests

if (!input.value) {
input.value = input.defaultValue;

Check warning on line 259 in packages/graphiql/src/components/GraphiQL.tsx

View check run for this annotation

Codecov / codecov/patch

packages/graphiql/src/components/GraphiQL.tsx#L259

Added line #L259 was not covered by tests
// @ts-expect-error
handleTabValueChange(event);

Check warning on line 261 in packages/graphiql/src/components/GraphiQL.tsx

View check run for this annotation

Codecov / codecov/patch

packages/graphiql/src/components/GraphiQL.tsx#L261

Added line #L261 was not covered by tests
}
input.blur();

Check warning on line 263 in packages/graphiql/src/components/GraphiQL.tsx

View check run for this annotation

Codecov / codecov/patch

packages/graphiql/src/components/GraphiQL.tsx#L263

Added line #L263 was not covered by tests
};

const handleTabDoubleClickAndBlur = (
event: MouseEvent<HTMLInputElement> | FocusEvent<HTMLInputElement>,
) => {
const input = event.currentTarget;
input.readOnly = event.type === 'blur';

Check warning on line 270 in packages/graphiql/src/components/GraphiQL.tsx

View check run for this annotation

Codecov / codecov/patch

packages/graphiql/src/components/GraphiQL.tsx#L268-L270

Added lines #L268 - L270 were not covered by tests
};

export function GraphiQLInterface(props: GraphiQLInterfaceProps) {
const isHeadersEditorEnabled = props.isHeadersEditorEnabled ?? true;
const editorContext = useEditorContext({ nonNull: true });
Expand Down Expand Up @@ -490,7 +520,7 @@
[confirmClose, editorContext, executionContext],
);

const handleTabClick: MouseEventHandler<HTMLButtonElement> = useCallback(
const handleTabClick: MouseEventHandler<HTMLInputElement> = useCallback(
event => {
const index = Number(
event.currentTarget.id.replace(TAB_CLASS_PREFIX, ''),
Expand Down Expand Up @@ -594,14 +624,25 @@
value={tab}
isActive={index === editorContext.activeTabIndex}
>
<Tab.Button
<input
key={tab.title}
className="graphiql-tab-input"
aria-controls="graphiql-session"
id={`graphiql-session-tab-${index}`}
title={tab.title}
defaultValue={tab.title}
size={Math.max(tab.title.length - 1, 1)}
onChange={handleTabValueChange}
onKeyDown={handleTabValueKeyDown}
onDoubleClick={handleTabDoubleClickAndBlur}
onBlur={handleTabDoubleClickAndBlur}
onClick={handleTabClick}
>
{tab.title}
</Tab.Button>
// Can be writable only after double click
readOnly
// Disable autocomplete for tab names
autoComplete="off"
/>

{tabs.length > 1 && <Tab.Close onClick={handleTabClose} />}
</Tab>
))}
Expand Down
3 changes: 0 additions & 3 deletions packages/graphiql/vite.config.mts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { defineConfig, PluginOption } from 'vite';
import packageJSON from './package.json';
import dts from 'vite-plugin-dts';
import commonjs from 'vite-plugin-commonjs';

const umdConfig = defineConfig({
define: {
Expand All @@ -11,8 +10,6 @@ const umdConfig = defineConfig({
'globalThis.process': 'true',
'process.env.NODE_ENV': '"production"',
},
// To bundle `const { createClient } = require('graphql-ws')` in `createWebsocketsFetcherFromUrl` function
plugins: [commonjs()],
build: {
minify: 'terser', // produce less bundle size
sourcemap: true,
Expand Down
Loading