Skip to content

Commit

Permalink
feat(CodeSnippet): support disabled single and multiline snippets (#7478
Browse files Browse the repository at this point in the history
)

* 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
  • Loading branch information
emyarod authored Jan 19, 2021
1 parent 07e3e1d commit ec9febb
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 27 deletions.
29 changes: 24 additions & 5 deletions packages/components/src/components/code-snippet/_code-snippet.scss
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,27 @@
@include reset;
}

.#{$prefix}--snippet--disabled,
.#{$prefix}--snippet--disabled
.#{$prefix}--btn.#{$prefix}--snippet-btn--expand {
color: $disabled-02;
background-color: $disabled-01;
}

.#{$prefix}--snippet--disabled .#{$prefix}--snippet-btn--expand:hover,
.#{$prefix}--snippet--disabled .#{$prefix}--copy-btn:hover {
color: $disabled-02;
background-color: $disabled-01;
cursor: not-allowed;
}

.#{$prefix}--snippet--disabled .#{$prefix}--snippet__icon,
.#{$prefix}--snippet--disabled
.#{$prefix}--snippet-btn--expand
.#{$prefix}--icon-chevron--down {
fill: $disabled-02;
}

.#{$prefix}--snippet code {
@include type-style('code-01');
}
Expand Down Expand Up @@ -319,7 +340,7 @@
}

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

Expand All @@ -335,8 +356,7 @@
border: 0;
}

button.#{$prefix}--btn.#{$prefix}--snippet-btn--expand
.#{$prefix}--snippet-btn--text {
.#{$prefix}--snippet-btn--expand .#{$prefix}--snippet-btn--text {
position: relative;
top: rem(-1px);
}
Expand All @@ -346,14 +366,13 @@
}

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

button.#{$prefix}--btn.#{$prefix}--snippet-btn--expand:hover {
.#{$prefix}--snippet-btn--expand:hover {
color: $text-01;
background: $hover-ui;
}
Expand Down
3 changes: 3 additions & 0 deletions packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,9 @@ Map {
"copyLabel": Object {
"type": "string",
},
"disabled": Object {
"type": "bool",
},
"feedback": Object {
"type": "string",
},
Expand Down
43 changes: 22 additions & 21 deletions packages/react/src/components/CodeSnippet/CodeSnippet-story.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,28 @@ export default {
},
};

const props = () => ({
type: select(
'Type (type)',
{
inline: 'inline',
'single line': 'single',
'multiple line': 'multi',
},
'inline'
),
disabled: boolean('Disabled (disabled)', false),
light: boolean('Light variant (light)', false),
feedback: text('Feedback text', 'Copied to clipboard'),
showMoreText: text('Text for "show more" button', 'Show more'),
showLessText: text('Text for "show less" button', 'Show less'),
hideCopyButton: boolean('Hide copy button (hideCopyButton)', false),
onClick: action('onClick'),
copyButtonDescription: text('Copy button title', 'Copy code snippet'),
ariaLabel: text('ARIA label', 'Container label'),
wrapText: boolean('Wrap text (wrapText)', true),
});

export const inline = () => (
<CodeSnippet type="inline" feedback="Copied to clipboard">
{'node -v'}
Expand Down Expand Up @@ -97,27 +119,6 @@ const lightPropMessage = (
</small>
);

const props = () => ({
type: select(
'Type',
{
inline: 'inline',
'single line': 'single',
'multiple line': 'multi',
},
'inline'
),
light: boolean('Light variant', false),
feedback: text('Feedback text', 'Copied to clipboard'),
showMoreText: text('Text for "show more" button', 'Show more'),
showLessText: text('Text for "show less" button', 'Show less'),
hideCopyButton: boolean('Hide copy button', false),
onClick: action('onClick'),
copyButtonDescription: text('Copy button title', 'Copy code snippet'),
ariaLabel: text('ARIA label', 'Container label'),
wrapText: boolean('Wrap text', true),
});

export const playground = () => (
<div className={props().light ? 'bx--tile' : ''}>
{props().light && lightPropMessage}
Expand Down
11 changes: 10 additions & 1 deletion packages/react/src/components/CodeSnippet/CodeSnippet.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function CodeSnippet({
className,
type,
children,
disabled,
feedback,
onClick,
ariaLabel,
Expand Down Expand Up @@ -110,6 +111,7 @@ function CodeSnippet({

const codeSnippetClasses = classNames(className, `${prefix}--snippet`, {
[`${prefix}--snippet--${type}`]: type,
[`${prefix}--snippet--disabled`]: type !== 'inline' && disabled,
[`${prefix}--snippet--expand`]: expandedCode,
[`${prefix}--snippet--light`]: light,
[`${prefix}--snippet--no-copy`]: hideCopyButton,
Expand Down Expand Up @@ -145,7 +147,7 @@ function CodeSnippet({
<div
ref={codeContainerRef}
role={type === 'single' ? 'textbox' : null}
tabIndex={type === 'single' ? 0 : null}
tabIndex={type === 'single' && !disabled ? 0 : null}
className={`${prefix}--snippet-container`}
aria-label={ariaLabel || copyLabel || 'code-snippet'}
onScroll={(type === 'single' && handleScroll) || null}>
Expand All @@ -169,6 +171,7 @@ function CodeSnippet({
)}
{!hideCopyButton && (
<CopyButton
disabled={disabled}
onClick={onClick}
feedback={feedback}
iconDescription={copyButtonDescription}
Expand All @@ -179,6 +182,7 @@ function CodeSnippet({
kind="ghost"
size="field"
className={`${prefix}--snippet-btn--expand`}
disabled={disabled}
onClick={() => setExpandedCode(!expandedCode)}>
<span className={`${prefix}--snippet-btn--text`}>
{expandCodeBtnText}
Expand Down Expand Up @@ -223,6 +227,11 @@ CodeSnippet.propTypes = {
*/
copyLabel: PropTypes.string,

/**
* Specify whether or not the CodeSnippet should be disabled
*/
disabled: PropTypes.bool,

/**
* Specify the string displayed when the snippet is copied
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ describe('Code Snippet', () => {
snippet.setProps({ hideCopyButton: true });
expect(snippet.find(CopyButton).length).toBe(0);
});

it('should set disabled if one is passed via props', () => {
snippet.setProps({ disabled: true });
expect(snippet.find(`.${prefix}--snippet--disabled`).length).toBe(1);
});
});

describe('Triggers appropriate events', () => {
Expand Down

0 comments on commit ec9febb

Please sign in to comment.