Skip to content

Commit ec9febb

Browse files
authored
feat(CodeSnippet): support disabled single and multiline snippets (#7478)
* docs(CodeSnippet): define props before calling * docs(CodeSnippet): add disabled knob * docs(CodeSnippet): add prop names * test(CodeSnippet): add test for `disabled` prop * feat(CodeSnippet): support disabled single and multiline snippets * chore: update snapshots * fix(CodeSnippet): remove tabindex on disabled snippet
1 parent 07e3e1d commit ec9febb

File tree

5 files changed

+64
-27
lines changed

5 files changed

+64
-27
lines changed

packages/components/src/components/code-snippet/_code-snippet.scss

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,27 @@
2323
@include reset;
2424
}
2525

26+
.#{$prefix}--snippet--disabled,
27+
.#{$prefix}--snippet--disabled
28+
.#{$prefix}--btn.#{$prefix}--snippet-btn--expand {
29+
color: $disabled-02;
30+
background-color: $disabled-01;
31+
}
32+
33+
.#{$prefix}--snippet--disabled .#{$prefix}--snippet-btn--expand:hover,
34+
.#{$prefix}--snippet--disabled .#{$prefix}--copy-btn:hover {
35+
color: $disabled-02;
36+
background-color: $disabled-01;
37+
cursor: not-allowed;
38+
}
39+
40+
.#{$prefix}--snippet--disabled .#{$prefix}--snippet__icon,
41+
.#{$prefix}--snippet--disabled
42+
.#{$prefix}--snippet-btn--expand
43+
.#{$prefix}--icon-chevron--down {
44+
fill: $disabled-02;
45+
}
46+
2647
.#{$prefix}--snippet code {
2748
@include type-style('code-01');
2849
}
@@ -319,7 +340,7 @@
319340
}
320341

321342
// Show more / less button
322-
button.#{$prefix}--btn.#{$prefix}--snippet-btn--expand {
343+
.#{$prefix}--snippet-btn--expand {
323344
@include type-style('body-short-01');
324345
@include carbon--font-family('sans');
325346

@@ -335,8 +356,7 @@
335356
border: 0;
336357
}
337358

338-
button.#{$prefix}--btn.#{$prefix}--snippet-btn--expand
339-
.#{$prefix}--snippet-btn--text {
359+
.#{$prefix}--snippet-btn--expand .#{$prefix}--snippet-btn--text {
340360
position: relative;
341361
top: rem(-1px);
342362
}
@@ -346,14 +366,13 @@
346366
}
347367

348368
.#{$prefix}--snippet-btn--expand .#{$prefix}--icon-chevron--down {
349-
margin-bottom: rem(1px);
350369
margin-left: $spacing-03;
351370
transform: rotate(0deg);
352371
transition: $duration--moderate-01 motion(standard, productive);
353372
fill: $text-01;
354373
}
355374

356-
button.#{$prefix}--btn.#{$prefix}--snippet-btn--expand:hover {
375+
.#{$prefix}--snippet-btn--expand:hover {
357376
color: $text-01;
358377
background: $hover-ui;
359378
}

packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,9 @@ Map {
356356
"copyLabel": Object {
357357
"type": "string",
358358
},
359+
"disabled": Object {
360+
"type": "bool",
361+
},
359362
"feedback": Object {
360363
"type": "string",
361364
},

packages/react/src/components/CodeSnippet/CodeSnippet-story.js

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,28 @@ export default {
2323
},
2424
};
2525

26+
const props = () => ({
27+
type: select(
28+
'Type (type)',
29+
{
30+
inline: 'inline',
31+
'single line': 'single',
32+
'multiple line': 'multi',
33+
},
34+
'inline'
35+
),
36+
disabled: boolean('Disabled (disabled)', false),
37+
light: boolean('Light variant (light)', false),
38+
feedback: text('Feedback text', 'Copied to clipboard'),
39+
showMoreText: text('Text for "show more" button', 'Show more'),
40+
showLessText: text('Text for "show less" button', 'Show less'),
41+
hideCopyButton: boolean('Hide copy button (hideCopyButton)', false),
42+
onClick: action('onClick'),
43+
copyButtonDescription: text('Copy button title', 'Copy code snippet'),
44+
ariaLabel: text('ARIA label', 'Container label'),
45+
wrapText: boolean('Wrap text (wrapText)', true),
46+
});
47+
2648
export const inline = () => (
2749
<CodeSnippet type="inline" feedback="Copied to clipboard">
2850
{'node -v'}
@@ -97,27 +119,6 @@ const lightPropMessage = (
97119
</small>
98120
);
99121

100-
const props = () => ({
101-
type: select(
102-
'Type',
103-
{
104-
inline: 'inline',
105-
'single line': 'single',
106-
'multiple line': 'multi',
107-
},
108-
'inline'
109-
),
110-
light: boolean('Light variant', false),
111-
feedback: text('Feedback text', 'Copied to clipboard'),
112-
showMoreText: text('Text for "show more" button', 'Show more'),
113-
showLessText: text('Text for "show less" button', 'Show less'),
114-
hideCopyButton: boolean('Hide copy button', false),
115-
onClick: action('onClick'),
116-
copyButtonDescription: text('Copy button title', 'Copy code snippet'),
117-
ariaLabel: text('ARIA label', 'Container label'),
118-
wrapText: boolean('Wrap text', true),
119-
});
120-
121122
export const playground = () => (
122123
<div className={props().light ? 'bx--tile' : ''}>
123124
{props().light && lightPropMessage}

packages/react/src/components/CodeSnippet/CodeSnippet.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ function CodeSnippet({
2323
className,
2424
type,
2525
children,
26+
disabled,
2627
feedback,
2728
onClick,
2829
ariaLabel,
@@ -110,6 +111,7 @@ function CodeSnippet({
110111

111112
const codeSnippetClasses = classNames(className, `${prefix}--snippet`, {
112113
[`${prefix}--snippet--${type}`]: type,
114+
[`${prefix}--snippet--disabled`]: type !== 'inline' && disabled,
113115
[`${prefix}--snippet--expand`]: expandedCode,
114116
[`${prefix}--snippet--light`]: light,
115117
[`${prefix}--snippet--no-copy`]: hideCopyButton,
@@ -145,7 +147,7 @@ function CodeSnippet({
145147
<div
146148
ref={codeContainerRef}
147149
role={type === 'single' ? 'textbox' : null}
148-
tabIndex={type === 'single' ? 0 : null}
150+
tabIndex={type === 'single' && !disabled ? 0 : null}
149151
className={`${prefix}--snippet-container`}
150152
aria-label={ariaLabel || copyLabel || 'code-snippet'}
151153
onScroll={(type === 'single' && handleScroll) || null}>
@@ -169,6 +171,7 @@ function CodeSnippet({
169171
)}
170172
{!hideCopyButton && (
171173
<CopyButton
174+
disabled={disabled}
172175
onClick={onClick}
173176
feedback={feedback}
174177
iconDescription={copyButtonDescription}
@@ -179,6 +182,7 @@ function CodeSnippet({
179182
kind="ghost"
180183
size="field"
181184
className={`${prefix}--snippet-btn--expand`}
185+
disabled={disabled}
182186
onClick={() => setExpandedCode(!expandedCode)}>
183187
<span className={`${prefix}--snippet-btn--text`}>
184188
{expandCodeBtnText}
@@ -223,6 +227,11 @@ CodeSnippet.propTypes = {
223227
*/
224228
copyLabel: PropTypes.string,
225229

230+
/**
231+
* Specify whether or not the CodeSnippet should be disabled
232+
*/
233+
disabled: PropTypes.bool,
234+
226235
/**
227236
* Specify the string displayed when the snippet is copied
228237
*/

packages/react/src/components/CodeSnippet/__tests__/CodeSnippet-test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ describe('Code Snippet', () => {
4444
snippet.setProps({ hideCopyButton: true });
4545
expect(snippet.find(CopyButton).length).toBe(0);
4646
});
47+
48+
it('should set disabled if one is passed via props', () => {
49+
snippet.setProps({ disabled: true });
50+
expect(snippet.find(`.${prefix}--snippet--disabled`).length).toBe(1);
51+
});
4752
});
4853

4954
describe('Triggers appropriate events', () => {

0 commit comments

Comments
 (0)