-
Notifications
You must be signed in to change notification settings - Fork 13
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(chip): add the chip component #10
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
label: Component | ||
title: Chip | ||
--- | ||
|
||
<page-intro>Filter chips use tags or descriptive words to filter content. Filter chips clearly delineate and display options in a compact area.</page-intro> | ||
|
||
<component | ||
component="chip" | ||
variation="chip" | ||
> | ||
</component> |
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 @@ | ||
<div class="ray-chip">Neighborhoods</div> |
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,46 @@ | ||
@import '../../global/variables'; | ||
|
||
$ray-chip-border-width: 1px; | ||
$ray-chip-height: $ray-spacing-xs * 2 + 1rem; | ||
|
||
.#{$ray-class-prefix}chip { | ||
@include no-select; | ||
border: $ray-chip-border-width solid $ray-color-blue-50; | ||
background-color: $ray-color-white; | ||
padding: $ray-spacing-xs $ray-spacing-md; | ||
border-radius: $ray-chip-height; | ||
color: $ray-color-blue-50; | ||
display: inline-block; | ||
cursor: pointer; | ||
|
||
&:not(:last-child) { | ||
margin-right: $ray-spacing-xs; | ||
} | ||
|
||
&:hover { | ||
background-color: $ray-color-blue-10; | ||
color: $ray-color-blue-50; | ||
} | ||
|
||
&:focus { | ||
box-shadow: $ray-box-shadow-focus-state; | ||
} | ||
|
||
&:active { | ||
background-color: $ray-color-blue-20; | ||
} | ||
} | ||
|
||
.#{$ray-class-prefix}chip--active { | ||
background-color: $ray-color-blue-50; | ||
color: $ray-color-white; | ||
|
||
&:hover { | ||
background-color: $ray-color-blue-50; | ||
color: $ray-color-white; | ||
} | ||
} | ||
|
||
.#{$ray-class-prefix}chip--micro { | ||
padding: 0 0.75rem; | ||
} |
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,78 @@ | ||
class Chip { | ||
static instances = new WeakMap(); | ||
|
||
static get cssClasses() { | ||
return { | ||
ACTIVE: 'ray-chip--active' | ||
}; | ||
} | ||
|
||
static get strings() { | ||
return { | ||
INIT_SELECTOR: '.ray-chip' | ||
}; | ||
} | ||
|
||
static create(element, options) { | ||
return this.instances.get(element) || new this(element, options); | ||
} | ||
|
||
static createAll(target = document, _options = { active: false }) { | ||
// Finds all instances of select on the document or within a given element and instantiates them. | ||
const options = { | ||
initSelector: this.strings.INIT_SELECTOR, | ||
..._options | ||
}; | ||
|
||
const chips = Array.from(target.querySelectorAll(options.initSelector)); | ||
chips.forEach(select => this.create(select, options)); | ||
} | ||
|
||
constructor(root, options) { | ||
this._root = root; | ||
this._options = options; | ||
|
||
this.constructor.instances.set(this._root, this); | ||
|
||
this.state = { | ||
active: | ||
options.active || | ||
this._root.classList.contains(this.constructor.cssClasses.ACTIVE) | ||
}; | ||
|
||
this._bindEventListeners(); | ||
this.assignClasses(); | ||
} | ||
|
||
_bindEventListeners() { | ||
this._root.addEventListener('mousedown', this.onMousedown); | ||
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. Thoughts on adding a |
||
} | ||
|
||
assignClasses() { | ||
if (this.state.active) { | ||
this._root.classList.add(this.constructor.cssClasses.ACTIVE); | ||
} else { | ||
this._root.classList.remove(this.constructor.cssClasses.ACTIVE); | ||
} | ||
} | ||
adamraider marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
setState(newState) { | ||
this.state = Object.assign({}, this.state, newState); | ||
this.assignClasses(); | ||
} | ||
|
||
onMousedown = () => { | ||
this.state.active = !this.state.active; | ||
this.assignClasses(); | ||
}; | ||
|
||
destroy() { | ||
// Implement this method to release any resources / deregister any listeners they have | ||
// attached. An example of this might be deregistering a resize event from the window object. | ||
this._root.removeEventListener('mousedown', this.onMousedown); | ||
|
||
this.constructor.instances.delete(this._root); | ||
} | ||
} | ||
|
||
export default Chip; |
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,4 +1,5 @@ | ||
import Select from './select'; | ||
import { TextField, TextArea } from './text-field'; | ||
import Chip from './chip'; | ||
|
||
export { Select, TextField, TextArea }; | ||
export { Select, TextField, TextArea, Chip }; |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import React from 'react'; | ||
import { storiesOf } from '@storybook/react'; | ||
|
||
import Chip from '../src/components/chip'; | ||
|
||
function initChip() { | ||
Chip.createAll(); | ||
} | ||
|
||
/* eslint-disable no-script-url */ | ||
storiesOf('Chip', module).add('default', () => { | ||
setTimeout(initChip); | ||
return ( | ||
<> | ||
<div className="ray-chip">Neighborhoods</div> | ||
<div className="ray-chip ray-chip--active">Offices</div> | ||
</> | ||
); | ||
}); | ||
storiesOf('Chip', module).add('micro', () => { | ||
setTimeout(initChip); | ||
return ( | ||
<> | ||
<div className="ray-chip ray-chip--micro">Offices</div> | ||
<div className="ray-chip ray-chip--micro ray-chip--active">Offices</div> | ||
</> | ||
); | ||
}); |
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 @@ | ||
import Chip from '../../src/components/chip'; | ||
|
||
describe('Chip', () => { | ||
test('stufff', () => { | ||
expect(Chip.create()).toBe(''); | ||
}); | ||
}); |
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 |
---|---|---|
|
@@ -59,6 +59,9 @@ | |
"card": { | ||
"title": "Card" | ||
}, | ||
"chip": { | ||
"title": "Chip" | ||
}, | ||
"image": { | ||
"title": "Image" | ||
}, | ||
|
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.
This needs tests before we merge.