-
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
Merged
alisonjoseph
merged 12 commits into
carbon-design-system:v9
from
alisonjoseph:code-snippet
May 16, 2018
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
85979f6
feat(code-snippet): Update code snippet styles and names
alisonjoseph e0e6b93
feat(code-snippet): Update code snippet styles
alisonjoseph be74044
feat(code-snippet): Update font size
alisonjoseph 58acdd5
chore: cleanup html files
alisonjoseph ecab0ff
feat(code-snippet): Update feedback copy tooltip to match new styles
alisonjoseph 4d38a1c
chore: cleanup javascript
alisonjoseph 201b4b9
feat(code-snippet): Update copy tooltip styles
alisonjoseph 886a0a3
chore: cleanup js
alisonjoseph a664504
feat(code-snippet): Update click event to only happen on button
alisonjoseph 904ba73
chore: Add back support for deprecated class names
alisonjoseph 275d4fb
feat(code-snippet): Update tooltip styles and positioning
alisonjoseph be35644
fix: update tooltip positioning for inline snippet
alisonjoseph File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 was deleted.
Oops, something went wrong.
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> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Let’s put a guard against NPE (when
expandBtn
does not exist), by checking the existence here.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.
For better explanation - Here's where the idea came from: #12
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.
👍