Skip to content

Commit

Permalink
[graphiql] fix placeholder ⌘ K in doc explorer search input for non…
Browse files Browse the repository at this point in the history
… mac devices, replace by `Ctrl K` (#3751)
  • Loading branch information
dimaMachina authored Aug 24, 2024
1 parent 1a561b3 commit b8538d8
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 23 deletions.
10 changes: 10 additions & 0 deletions .changeset/lemon-mirrors-accept.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'codemirror-graphql': patch
'@graphiql/react': patch
'cm6-graphql': patch
'graphiql': patch
---

replace deprecated `navigator.platform` with `navigator.userAgent`

fix placeholder `⌘ K` in doc explorer search input for non mac devices, replace by `Ctrl K`
2 changes: 1 addition & 1 deletion packages/cm6-graphql/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ export class Position implements IPosition {
}
}

const isMac = () => /mac/i.test(navigator.platform);
const isMac = () => navigator.userAgent.includes('Mac');
export const isMetaKeyPressed = (e: MouseEvent) =>
isMac() ? e.metaKey : e.ctrlKey;
2 changes: 1 addition & 1 deletion packages/codemirror-graphql/src/utils/jump-addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ function onKeyDown(cm: CodeMirror.Editor, event: KeyboardEvent) {
}

const isMac =
typeof navigator !== 'undefined' && navigator?.appVersion.includes('Mac');
typeof navigator !== 'undefined' && navigator.userAgent.includes('Mac');

function isJumpModifier(key: string) {
return key === (isMac ? 'Meta' : 'Control');
Expand Down
7 changes: 1 addition & 6 deletions packages/graphiql-react/src/editor/common.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { KeyMap } from './types';
import { isMacOs } from '../utility/is-macos';

export const DEFAULT_EDITOR_THEME = 'graphiql';
export const DEFAULT_KEY_MAP: KeyMap = 'sublime';

let isMacOs = false;

if (typeof window === 'object') {
isMacOs = window.navigator.platform.toLowerCase().indexOf('mac') === 0;
}

export const commonKeys = {
// Persistent search box in Query Editor
[isMacOs ? 'Cmd-F' : 'Ctrl-F']: 'findPersistent',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

&:not(:focus-within) [role='combobox'] {
height: 24px;
width: 4ch;
width: 5ch;
}

& [role='combobox']:focus {
Expand Down
3 changes: 2 additions & 1 deletion packages/graphiql-react/src/explorer/components/search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { useExplorerContext } from '../context';

import './search.css';
import { renderType } from './utils';
import { isMacOs } from '../../utility/is-macos';

export function Search() {
const { explorerNavStack, push } = useExplorerContext({
Expand Down Expand Up @@ -103,7 +104,7 @@ export function Search() {
onFocus={handleFocus}
onBlur={handleFocus}
onChange={event => setSearchValue(event.target.value)}
placeholder="⌘ K"
placeholder={`${isMacOs ? '⌘' : 'Ctrl'} K`}
ref={inputRef}
value={searchValue}
data-cy="doc-explorer-input"
Expand Down
1 change: 1 addition & 0 deletions packages/graphiql-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export {
} from './storage';
export { useTheme } from './theme';
export { useDragResize } from './utility/resize';
export { isMacOs } from './utility/is-macos';

export * from './icons';
export * from './ui';
Expand Down
2 changes: 2 additions & 0 deletions packages/graphiql-react/src/utility/is-macos.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const isMacOs =
typeof navigator !== 'undefined' && navigator.userAgent.includes('Mac');
7 changes: 2 additions & 5 deletions packages/graphiql/src/components/GraphiQL.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import {
UseVariableEditorArgs,
VariableEditor,
WriteableEditorProps,
isMacOs,
} from '@graphiql/react';

const majorVersion = parseInt(React.version.slice(0, 2), 10);
Expand Down Expand Up @@ -915,11 +916,7 @@ export function GraphiQLInterface(props: GraphiQLInterfaceProps) {
);
}

const modifier =
typeof window !== 'undefined' &&
window.navigator.platform.toLowerCase().indexOf('mac') === 0
? 'Cmd'
: 'Ctrl';
const modifier = isMacOs ? '⌘' : 'Ctrl';

const SHORT_KEYS = Object.entries({
'Search in editor': [modifier, 'F'],
Expand Down
12 changes: 4 additions & 8 deletions packages/graphiql/test/beforeDevServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,13 @@ const path = require('node:path');
// eslint-disable-next-line import-x/no-extraneous-dependencies
const { createHandler } = require('graphql-http/lib/use/express');
const schema = require('./schema');
const badSchema = require('../cypress/fixtures/bad-schema.json');
const { customExecute } = require('./execute');

module.exports = function beforeDevServer(app, _server, _compiler) {
// GraphQL Server
app.post('/graphql', createHandler({ schema }));
app.get('/graphql', createHandler({ schema }));

app.post('/bad/graphql', (_req, res, next) => {
res.json({ data: badSchema });
next();
});
const handler = createHandler({ schema, execute: customExecute });
app.post('/graphql', handler);
app.get('/graphql', handler);

app.use('/images', express.static(path.join(__dirname, 'images')));

Expand Down

0 comments on commit b8538d8

Please sign in to comment.