Skip to content
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
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
46 changes: 46 additions & 0 deletions packages/core/src/components/chip/_chip.scss
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;
}
78 changes: 78 additions & 0 deletions packages/core/src/components/chip/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
class Chip {
Copy link
Contributor Author

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.

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);
Copy link

@cmugla cmugla Apr 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts on adding a touchstart event as well? Not sure what the best UX is for this - seeing on my phone that they don't work very well for quicker, slightly clumsier gestures

}

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;
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
28 changes: 28 additions & 0 deletions packages/core/stories/chip.stories.js
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>
</>
);
});
7 changes: 7 additions & 0 deletions packages/core/test/components/chip.test.js
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('');
});
});
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 @@ -59,6 +59,9 @@
"card": {
"title": "Card"
},
"chip": {
"title": "Chip"
},
"image": {
"title": "Image"
},
Expand Down