-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(code-snippet): Add new Code snippet variation and styles (#761)
* feat(code-snippet): Update code snippet styles and names * feat(code-snippet): Update font size * chore: cleanup html files * feat(code-snippet): Update feedback copy tooltip to match new styles * chore: cleanup javascript * feat(code-snippet): Update copy tooltip styles * chore: cleanup js * feat(code-snippet): Update click event to only happen on button * chore: Add back support for deprecated class names * feat(code-snippet): Update tooltip styles and positioning * fix: update tooltip positioning for inline snippet
- Loading branch information
1 parent
2ab181b
commit 724de68
Showing
13 changed files
with
334 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
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'; | ||
|
||
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._initCodeSnippet(); | ||
this.element.querySelector(this.options.classExpandText).addEventListener('click', evt => this._handleClick(evt)); | ||
} | ||
|
||
_handleClick() { | ||
const expandBtn = this.element.querySelector(this.options.classExpandText); | ||
this.element.classList.toggle(this.options.classExpanded); | ||
|
||
if (this.element.classList.contains(this.options.classExpanded)) { | ||
expandBtn.textContent = expandBtn.getAttribute(this.options.attribShowLessText); | ||
} else { | ||
expandBtn.textContent = expandBtn.getAttribute(this.options.attribShowMoreText); | ||
} | ||
} | ||
|
||
_initCodeSnippet() { | ||
const expandBtn = this.element.querySelector(this.options.classExpandText); | ||
if (!expandBtn) { | ||
throw new TypeError('Cannot find the expand button.'); | ||
} | ||
|
||
expandBtn.textContent = expandBtn.getAttribute(this.options.attribShowMoreText); | ||
|
||
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]', | ||
attribShowMoreText: 'data-show-more-text', | ||
attribShowLessText: '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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<!-- 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
### HTML | ||
|
||
Terminal snippet has been renamed to Single line snippet, Code snippet has been renamed to Multi-line snippet. We have also added a new variation, Inline snippet. | ||
|
||
### SCSS | ||
|
||
|Old Class |New Class | Note | | ||
|------------------------|------------------------|---------| | ||
|`bx--snippet--terminal` |`bx--snippet--single` | Changed | | ||
|`bx--snippet--code` |`bx--snippet--multi` | Changed | | ||
| |`bx--snippet--inline` | New | | ||
|
||
|
||
### Javascript | ||
|
||
The multi-line code snippet now has javascript. Be sure to add the data attribute `[data-code-snippet]` for it to work. |
Oops, something went wrong.