Skip to content

Commit d27a361

Browse files
authored
Add variant to KeyboardKey component (#8288)
<!-- ☝️How to write a good PR title: - Prefix it with [ComponentName] (if applicable), for example: [Button] - Start with a verb, for example: Add, Delete, Improve, Fix… - Give as much context as necessary and as little as possible - Prefix it with [WIP] while it’s a work in progress --> ### WHY are these changes introduced? Part of the Online store editor Keyboard Shortcut enhancement - [Figma](https://www.figma.com/file/qRogDRYEpj1CokjAY06WKi/OSE-Keyboard-Shortcuts?node-id=1734%3A15442) Update styling of the default `KeyboardKey` component and adds optional `size` prop. <details open> <summary>Screenshots</summary> <br> Before: <img width="79" alt="image" src="https://user-images.githubusercontent.com/24706433/223430091-cbeefee6-4843-4c4d-8ffc-edf6b1dd01c5.png"> ### Change Default: <img width="460" alt="image" src="https://user-images.githubusercontent.com/24706433/223429825-381e6be6-a7ff-42fb-a95e-6d9e9aa197e8.png"> Small: <img width="307" alt="image" src="https://user-images.githubusercontent.com/24706433/223895796-a835346f-9a42-448c-9b9c-0c408995da5d.png"> </details> <!-- Context about the problem that’s being addressed. --> ### WHAT is this pull request doing? <!-- Summary of the changes committed. Before / after screenshots are appreciated for UI changes. Make sure to include alt text that describes the screenshot. If you include an animated gif showing your change, wrapping it in a details tag is recommended. Gifs usually autoplay, which can cause accessibility issues for people reviewing your PR: <details> <summary>Summary of your gif(s)</summary> <img src="..." alt="Description of what the gif shows"> </details> --> Update `KeyboardKey` component to add an optional property `variant`, which can take `small` as a value. A `KeyboardKey` component with this variant will be used in tooltips to display the keyboard shortcut of an action. <!-- ℹ️ Delete the following for small / trivial changes --> ### How to 🎩 🖥 [Local development instructions](https://github.com/Shopify/polaris/blob/main/README.md#local-development) 🗒 [General tophatting guidelines](https://github.com/Shopify/polaris/blob/main/documentation/Tophatting.md) 📄 [Changelog guidelines](https://github.com/Shopify/polaris/blob/main/.github/CONTRIBUTING.md#changelog) <!-- Give as much information as needed to experiment with the component in the playground. --> <details> <summary>Copy-paste this code in <code>playground/Playground.tsx</code>:</summary> ```jsx import React from 'react'; import {Page} from '../src'; export function Playground() { return ( <Page title="Playground"> {/* Add the code you want to test in here */} </Page> ); } ``` </details> ### 🎩 checklist - [ ] Tested on [mobile](https://github.com/Shopify/polaris/blob/main/documentation/Tophatting.md#cross-browser-testing) - [ ] Tested on [multiple browsers](https://help.shopify.com/en/manual/shopify-admin/supported-browsers) - [x] Tested for [accessibility](https://github.com/Shopify/polaris/blob/main/documentation/Accessibility%20testing.md) - [x] [Tophatted documentation](https://github.com/Shopify/polaris/blob/main/documentation/Tophatting%20documentation.md) changes in the style guide
1 parent c820763 commit d27a361

File tree

5 files changed

+107
-20
lines changed

5 files changed

+107
-20
lines changed

.changeset/quiet-mirrors-talk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@shopify/polaris': minor
3+
---
4+
5+
Updated the style of keyboard component and add optional size prop
Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
1-
$key-base-height: 24px;
1+
$key-base-dimension: 28px;
22

33
.KeyboardKey {
44
// stylelint-disable-next-line -- generated by polaris-migrator DO NOT COPY
5-
display: inline-block;
6-
// stylelint-disable-next-line -- generated by polaris-migrator DO NOT COPY
7-
height: $key-base-height;
5+
height: $key-base-dimension;
6+
display: inline-flex;
7+
justify-content: center;
88
margin: 0 var(--p-space-05) var(--p-space-05);
9-
padding: 0 var(--p-space-1);
10-
background-color: var(--p-surface);
11-
box-shadow: 0 0 0 var(--p-space-025) var(--p-border-subdued),
12-
0 var(--p-space-05) 0 0 var(--p-surface),
13-
0 var(--p-space-05) 0 var(--p-space-025) var(--p-border-subdued);
9+
padding: 0 var(--p-space-2);
10+
background: var(--p-surface-depressed);
11+
box-shadow: 0 var(--p-space-025) 0 var(--p-hint-from-direct-light);
1412
border-radius: var(--p-border-radius-1);
15-
font-family: var(--p-font-family-sans);
16-
font-size: var(--p-font-size-75);
13+
color: var(--p-text);
14+
font-size: var(--p-font-size-100);
1715
font-weight: var(--p-font-weight-medium);
18-
line-height: $key-base-height;
19-
color: var(--p-text-subdued);
16+
font-family: var(--p-font-family-sans);
17+
line-height: $key-base-dimension;
2018
text-align: center;
21-
min-width: $key-base-height;
19+
min-width: $key-base-dimension;
2220
user-select: none;
2321
}
22+
23+
.small {
24+
background: var(--p-background-selected);
25+
border-radius: var(--p-border-radius-05);
26+
box-shadow: none;
27+
line-height: unset;
28+
padding: 0 var(--p-space-1);
29+
font-size: var(--p-font-size-75);
30+
height: var(--p-space-5);
31+
min-width: var(--p-space-5);
32+
}

polaris-react/src/components/KeyboardKey/KeyboardKey.stories.tsx

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,64 @@ export default {
77
} as ComponentMeta<typeof KeyboardKey>;
88

99
export function Default() {
10-
return <KeyboardKey>Ctrl</KeyboardKey>;
10+
return (
11+
<div style={{display: 'flex', gap: '20px'}}>
12+
<div style={{display: 'flex', flexDirection: 'column'}}>
13+
<div style={{display: 'flex'}}>
14+
<KeyboardKey></KeyboardKey>
15+
<KeyboardKey>s</KeyboardKey>
16+
</div>
17+
<div style={{display: 'flex'}}>
18+
<KeyboardKey>ctrl</KeyboardKey>
19+
<KeyboardKey>s</KeyboardKey>
20+
</div>
21+
</div>
22+
23+
<div style={{display: 'flex', flexDirection: 'column'}}>
24+
<div style={{display: 'flex'}}>
25+
<KeyboardKey></KeyboardKey>
26+
<KeyboardKey>/</KeyboardKey>
27+
</div>
28+
<div style={{display: 'flex'}}>
29+
<KeyboardKey>ctrl</KeyboardKey>
30+
<KeyboardKey>/</KeyboardKey>
31+
</div>
32+
</div>
33+
</div>
34+
);
35+
}
36+
37+
export function Small() {
38+
return (
39+
<div style={{display: 'flex', gap: '20px'}}>
40+
<div style={{display: 'flex', flexDirection: 'column'}}>
41+
<div style={{display: 'flex'}}>
42+
<KeyboardKey size="small"></KeyboardKey>
43+
<KeyboardKey size="small">d</KeyboardKey>
44+
</div>
45+
<div style={{display: 'flex'}}>
46+
<KeyboardKey size="small"></KeyboardKey>
47+
<KeyboardKey size="small">h</KeyboardKey>
48+
</div>
49+
<div style={{display: 'flex'}}>
50+
<KeyboardKey size="small"></KeyboardKey>
51+
<KeyboardKey size="small"></KeyboardKey>
52+
</div>
53+
</div>
54+
<div style={{display: 'flex', flexDirection: 'column'}}>
55+
<div style={{display: 'flex'}}>
56+
<KeyboardKey size="small">ctrl</KeyboardKey>
57+
<KeyboardKey size="small">d</KeyboardKey>
58+
</div>
59+
<div style={{display: 'flex'}}>
60+
<KeyboardKey size="small">ctrl</KeyboardKey>
61+
<KeyboardKey size="small">h</KeyboardKey>
62+
</div>
63+
<div style={{display: 'flex'}}>
64+
<KeyboardKey size="small">ctrl</KeyboardKey>
65+
<KeyboardKey size="small"></KeyboardKey>
66+
</div>
67+
</div>
68+
</div>
69+
);
1170
}
Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
import React from 'react';
22

3+
import {classNames} from '../../utilities/css';
4+
35
import styles from './KeyboardKey.scss';
46

7+
type Size = 'small';
8+
59
export interface KeyboardKeyProps {
6-
/** The content to display inside the key */
710
children?: string;
11+
size?: Size;
812
}
13+
export function KeyboardKey({children = '', size}: KeyboardKeyProps) {
14+
const key =
15+
!size && children.length > 1
16+
? children.toLowerCase()
17+
: children.toUpperCase();
918

10-
export function KeyboardKey({children}: KeyboardKeyProps) {
11-
let key = children || '';
12-
key = key.length > 1 ? key.toLowerCase() : key.toUpperCase();
19+
const className = classNames(styles.KeyboardKey, size && styles[size]);
1320

14-
return <kbd className={styles.KeyboardKey}>{key}</kbd>;
21+
return <kbd className={className}>{key}</kbd>;
1522
}

polaris-react/src/components/KeyboardKey/tests/KeyboardKey.test.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,11 @@ describe('<KeyboardKey />', () => {
2323
const keyboardKey = mountWithApp(<KeyboardKey />);
2424
expect(keyboardKey).toContainReactText('');
2525
});
26+
27+
it("renders small icon when size is set to 'small'", () => {
28+
const keyboardKey = mountWithApp(<KeyboardKey size="small" />);
29+
expect(keyboardKey).toContainReactComponent('kbd', {
30+
className: 'KeyboardKey small',
31+
});
32+
});
2633
});

0 commit comments

Comments
 (0)