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

feat(code-snippet): Add new Code snippet variation and styles #761

Merged
merged 12 commits into from
May 16, 2018
Merged
Show file tree
Hide file tree
Changes from 6 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
10 changes: 5 additions & 5 deletions demo/scss/_page.scss
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ td {
}
}

button {
border-radius: 0;
}

& > *:not(.component-example):not(.component-variation),
& > {
button {
border-radius: 0;
}

.page__divider-heading {
@include typescale('zeta');
font-weight: 600;
Expand Down Expand Up @@ -151,7 +151,7 @@ td {
display: block;
background-color: $brand-01;
position: absolute;
top: -.5rem;
top: -0.5rem;
left: 0;
}

Expand Down
7 changes: 4 additions & 3 deletions src/components/code-snippet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ Mixins specific to Code Snippet are located in [src/components/code-snippet/_mix

#### Modifiers

Use these modifiers with `.bx--root-class` class.
Use these modifiers with `.bx--snippet` class.

| Selector | Description |
|----------------------------------|------------------------------------|
| .bx--snippet--code | Styles for multiple lines of code |
| .bx--snippet--terminal | Styles for single lines of code |
| .bx--snippet--single | Styles for multiple lines of code |
| .bx--snippet--multi | Styles for single lines of code |
| .bx--snippet--inline | Styles for code inline inside text |
125 changes: 95 additions & 30 deletions src/components/code-snippet/_code-snippet.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,68 +8,102 @@
@import 'mixins';

@include exports('snippet') {
.#{$prefix}--snippet--code {
@include bx--snippet;
background-color: $snippet-background-color;
padding: $spacing-md $spacing-2xl $spacing-md $spacing-md;
}

.#{$prefix}--snippet code {
@include typescale('epsilon');
font-family: $font-family-mono;
}

.#{$prefix}--snippet--code .#{$prefix}--snippet-container {
max-height: rem(192px);
overflow-y: scroll;
margin-right: $spacing-md;
// Inline Code Snippet
.#{$prefix}--snippet--inline {
@include reset;
position: relative;
display: inline;
padding: 0;
background: transparent;
font-size: inherit;
border: 1px solid transparent;
border-radius: 4px;
background-color: $field-01;
font-size: 85%;
cursor: pointer;

&:hover {
background-color: darken($field-01, 10%);
}

&:focus {
@include focus-outline;
outline: none;
border: 1px solid $brand-01;
}
}

.#{$prefix}--snippet--code .#{$prefix}--snippet-container pre {
white-space: pre-wrap;
.#{$prefix}--snippet--inline code {
padding: rem(1px) $spacing-xs;
}

.#{$prefix}--snippet--terminal {
.#{$prefix}--snippet--inline.#{$prefix}--snippet--light {
background-color: $field-02;

&:hover {
background-color: darken($field-02, 10%);
}
}

// Single Line Snippet
.#{$prefix}--snippet--single {
@include bx--snippet;
background-color: $text-01;
color: $inverse-01;
height: 3.5rem;
height: rem(56px);
padding: 0 $spacing-2xl 0 $spacing-md;
}

.#{$prefix}--snippet--terminal .#{$prefix}--snippet-container {
.#{$prefix}--snippet--single .#{$prefix}--snippet-container {
display: flex;
align-items: center;
height: 130%;
white-space: nowrap;
overflow-x: scroll;
position: relative;
padding-bottom: $spacing-md;
}

// Multi Line Snippet
.#{$prefix}--snippet--multi {
@include bx--snippet;
padding: $spacing-md $spacing-3xl $spacing-md $spacing-lg;
}

.#{$prefix}--snippet--multi .#{$prefix}--snippet-container {
overflow: hidden;
position: relative;
max-height: rem(254px);
min-height: rem(56px);
transition: all $transition--base $carbon--standard-easing;
}

.#{$prefix}--snippet--expand.#{$prefix}--snippet--multi .#{$prefix}--snippet-container {
max-height: rem(1500px);
}

.#{$prefix}--snippet--multi .#{$prefix}--snippet-container pre {
white-space: pre-wrap;
}

.#{$prefix}--snippet__icon {
fill: $brand-01;
height: rem(24px);
width: rem(18px);
transition: $transition--base;
transition: all $transition--base $carbon--standard-easing;

&:hover {
fill: $brand-02;
}
}

.#{$prefix}--snippet--terminal .#{$prefix}--snippet__icon {
fill: $inverse-01;

&:hover {
fill: $brand-02;
fill: $hover-primary;
}
}

.#{$prefix}--snippet-button {
@include reset;
cursor: pointer;
position: absolute;
top: 0.5rem;
top: 0.675rem;
right: 0.5rem;
border: none;
background-color: transparent;
Expand All @@ -86,9 +120,40 @@
.#{$prefix}--snippet .#{$prefix}--btn--copy__feedback {
@include font-family;
z-index: z('overlay');
font-weight: 600;
font-weight: 400;
left: inherit;
top: 1rem;
right: 1.25rem;
}

.#{$prefix}--snippet--inline .#{$prefix}--btn--copy__feedback {
right: auto;
}

// Show more / less button
.#{$prefix}--snippet-btn--expand {
position: absolute;
right: 0;
bottom: 0;
}

.#{$prefix}--snippet-btn--expand--hide.#{$prefix}--snippet-btn--expand {
display: none;
}

.#{$prefix}--snippet-btn--expand .#{$prefix}--icon-chevron--down {
fill: $brand-01;
margin-left: $spacing-2xs;
transform: rotate(0deg);
transition: $transition--base;
}

.#{$prefix}--snippet-btn--expand:hover .#{$prefix}--icon-chevron--down,
.#{$prefix}--snippet-btn--expand:focus .#{$prefix}--icon-chevron--down {
fill: $inverse-01;
}

.#{$prefix}--snippet--expand .#{$prefix}--snippet-btn--expand .#{$prefix}--icon-chevron--down {
transform: rotate(180deg);
}
}
6 changes: 5 additions & 1 deletion src/components/code-snippet/_mixins.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
@mixin bx--snippet {
@include typescale('omega');
line-height: 1.45;
background: $snippet-background-color;
border: 1px solid $snippet-border-color;
font-family: $font-family-mono;
position: relative;
max-width: rem(640px);
max-width: rem(600px);
width: 100%;
}
32 changes: 0 additions & 32 deletions src/components/code-snippet/code-snippet.html

This file was deleted.

77 changes: 77 additions & 0 deletions src/components/code-snippet/code-snippet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import mixin from '../../globals/js/misc/mixin';
import createComponent from '../../globals/js/mixins/create-component';
import initComponentBySearch from '../../globals/js/mixins/init-component-by-search';
import handles from '../../globals/js/mixins/handles';
import on from '../../globals/js/misc/on';

class CodeSnippet extends mixin(createComponent, initComponentBySearch, handles) {
/**
* CodeSnippet UI.
* @extends CreateComponent
* @extends InitComponentBySearch
* @extends Handles
* @param {HTMLElement} element The element working as a CodeSnippet UI.
*/

constructor(element, options) {
super(element, options);
this.manage(
on(this.element, 'click', event => {
this._handleClick(event);
})
);

this._initCodeSnippet();
}

_handleClick() {
const expandBtn = this.element.querySelector(this.options.classExpandText);
this.element.classList.toggle(this.options.classExpanded);

if (this.element.classList.contains(this.options.classExpanded)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let’s put a guard against NPE (when expandBtn does not exist), by checking the existence here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For better explanation - Here's where the idea came from: #12

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

expandBtn.textContent = expandBtn.getAttribute(this.options.showLessText);
} else {
expandBtn.textContent = expandBtn.getAttribute(this.options.showMoreText);
}
}

_initCodeSnippet() {
const expandBtn = this.element.querySelector(this.options.classExpandText);

expandBtn.textContent = expandBtn.getAttribute(this.options.showMoreText);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let’s put a guard against NPE (when expandBtn does not exist), by checking the existence here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For better explanation - Here's where the idea came from: #12


if (this.element.offsetHeight < this.options.minHeight) {
this.element.classList.add(this.options.classHideExpand);
this.element.classList.add(this.options.classExpanded);
}
}

/**
* The map associating DOM element and code snippet UI instance.
* @member CodeSnippet.components
* @type {WeakMap}
*/
static components = new WeakMap();

/**
* The component options.
* If `options` is specified in the constructor, {@linkcode CodeSnippet.create .create()},
* or {@linkcode CodeSnippet.init .init()},
* properties in this object are overriden for the instance being create and how {@linkcode CodeSnippet.init .init()} works.
* @member CodeSnippet.options
* @type {Object}
* @property {string} selectorInit The data attribute to find code snippet UIs.
*/
static options = {
selectorInit: '[data-code-snippet]',
showMoreText: 'data-show-more-text',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let’s rename it to attribShowMoreText/attribShowLessText - Other components take this naming rule.

showLessText: 'data-show-less-text',
minHeight: 288,
classExpanded: 'bx--snippet--expand',
classExpandBtn: 'bx--snippet-btn--expand',
classExpandText: '.bx--snippet-btn--text',
classHideExpand: 'bx--snippet-btn--expand--hide',
};
}

export default CodeSnippet;
6 changes: 6 additions & 0 deletions src/components/code-snippet/inline-code-snippet-light.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<p>Here is an example of a text that a user would be reading. In this paragraph would be
<button data-copy-btn="" class="bx--snippet bx--snippet--inline bx--snippet--light" aria-label="Copy code" tabindex="0">
<code>inline code</code>
<div class="bx--btn--copy__feedback" data-feedback="Copied!"></div>
</button>
that the user could look at and copy in to their code editor.</p>
8 changes: 8 additions & 0 deletions src/components/code-snippet/inline-code-snippet.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

<!-- This should be on a white background -->
<p>Here is an example of a text that a user would be reading. In this paragraph would be
<button data-copy-btn="" class="bx--snippet bx--snippet--inline" aria-label="Copy code" tabindex="0">
<code>inline code</code>
<div class="bx--btn--copy__feedback" data-feedback="Copied!"></div>
</button>
that the user could look at and copy in to their code editor.</p>
13 changes: 13 additions & 0 deletions src/components/code-snippet/migrate-to-9.x.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
### HTML

Terminal snippet has been renamed to Single line snippet, Code snippet has been renamed to Multi-line snippet and we have added a new variation, Inline snippet.

### SCSS

|Old Class |New Class |
|------------------------|------------------------|
|`bx--snippet--terminal` |`bx--snippet--single` |
|`bx--snippet--code` |`bx--snippet--multi` |
| n/a | `bx--snippet--inline` |


Loading