-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Changes from 5 commits
85979f6
e0e6b93
be74044
58acdd5
ecab0ff
4d38a1c
201b4b9
886a0a3
a664504
904ba73
275d4fb
be35644
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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%; | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
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() { | ||
this.element.parentElement.classList.toggle('bx--snippet--expand'); | ||
|
||
if (this.element.parentElement.classList.contains('bx--snippet--expand')) { | ||
this.element.querySelectorAll('.bx--snippet-btn--text')[0].innerHTML = this.options.showLess; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} else { | ||
this.element.querySelectorAll('.bx--snippet-btn--text')[0].innerHTML = this.options.showMore; | ||
} | ||
} | ||
|
||
_initCodeSnippet() { | ||
this.element.querySelectorAll('.bx--snippet-btn--text')[0].innerHTML = this.options.showMore; | ||
if (this.element.parentElement.offsetHeight < 288) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great to do |
||
this.element.classList.add('bx--snippet-btn--expand--hide'); | ||
this.element.parentElement.classList.add('bx--snippet--expand'); | ||
} | ||
} | ||
|
||
/** | ||
* The map associating DOM element and copy button 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 copy button UIs. | ||
*/ | ||
static options = { | ||
selectorInit: '.bx--snippet--multi .bx--snippet-btn--expand', | ||
showMore: 'Show more', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great to embed those translatable strings in data attribute so the consumer’s code does not have to deal with our vanilla JavaScript code for translation - e.g. |
||
showLess: 'Show less', | ||
}; | ||
} | ||
|
||
export default CodeSnippet; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we really want the whitespace here…? |
||
<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> |
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> |
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` | | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great to define
classExpanded
option (value isbx--snippet--expand
) for more flexible custom theming, etc. Same for all classes/selectors.