Skip to content

Commit

Permalink
feat(chip): add the chip component
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Raider committed Jun 20, 2019
1 parent d07ecea commit 2baf420
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 1 deletion.
12 changes: 12 additions & 0 deletions docs/components/chip.md
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>
1 change: 1 addition & 0 deletions docs/html/chip/chip.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class="ray-chip">Neighborhoods</div>
42 changes: 42 additions & 0 deletions packages/core/src/components/chip/_chip.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
@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;
}
}
68 changes: 68 additions & 0 deletions packages/core/src/components/chip/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
class Chip {
static instances = new WeakMap();

static get cssClasses() {
return {};
}

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.constructor.instances.set(this._root, this);

this.state = {
active: options.active
};

this._bindEventListeners();
this.assignClasses();
}

_bindEventListeners() {
this._root.addEventListener('mousedown', this.onMousedown);
}

assignClasses() {
if (this.state.active) {
this._root.classList.add('ray-chip--active');
} else {
this._root.classList.remove('ray-chip--active');
}
}

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;
3 changes: 2 additions & 1 deletion packages/core/src/components/index.js
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 };
1 change: 1 addition & 0 deletions packages/core/src/ray-core.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
@import './global/material-grid/mdc-layout-grid';

@import './components/card/card';
@import './components/chip/chip';
@import './components/button/button';
@import './components/breadcrumb/breadcrumb';
@import './components/radio-checkbox/radio-checkbox';
Expand Down
19 changes: 19 additions & 0 deletions packages/core/stories/chip.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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>
</>
);
});
3 changes: 3 additions & 0 deletions packages/docs-app/src/data/navigation/navigation.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
"card": {
"title": "Card"
},
"chip": {
"title": "Chip"
},
"image": {
"title": "Image"
},
Expand Down

0 comments on commit 2baf420

Please sign in to comment.