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(docs): Interactive playground component #140

Merged
merged 15 commits into from
Mar 9, 2020
21 changes: 21 additions & 0 deletions packages/@nucleus/app/styles/doc/_icon.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// icon tiles
.icon-container {
display: flex;
flex-wrap: wrap;

.icon {
text-align: center;
padding: 10px 5px 5px;
margin: 5px;
width: 150px;
cursor: pointer;

&:hover {
background: #dae1e7;
}

&--text {
padding: 5px 0;
}
}
}
45 changes: 45 additions & 0 deletions packages/@nucleus/app/styles/doc/_playgroud.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
$playground-border-color: #dae1e7;

.playground-container {
display: flex;
border-bottom: 1px dotted $playground-border-color;
padding: 10px;
justify-content: space-around;

&--component {
align-self: center;
width: 50%;
text-align: center;
Copy link
Contributor

Choose a reason for hiding this comment

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

This might affect the childrens' text-align and get applied to nucleus-components. Needs some looking into

padding-right: 10px;
}

&--props {
padding-left: 10px;
border-left: 1px dotted $playground-border-color;
max-height: 25vh;
width: 50%;
overflow-y: auto;

.item {
margin: 10px;

&--label {
display: block;
padding-bottom: 5px;
font-weight: 700;
}
}
}

&--code {
position: relative;
padding: 20px;
}

&--code-copy {
position: absolute;
right: 0;
bottom: -1px;
padding: 0;
}
}
2 changes: 2 additions & 0 deletions packages/@nucleus/app/styles/nucleus-docs.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
@import "./doc/custom-addon-theme";
@import "./doc/custom-syntax-highlight";
@import "./doc/nucleus-mod";
@import "./doc/icon";
@import "./doc/playgroud";
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import Component from '@ember/component';
import { get, set, action } from "@ember/object";
import {
registerComputedProperties,
handlePropertyChange,
generateCode
} from '../../utils/playground';

const PROPERTIES = [
{
name: 'label',
input: true,
value: 'Click here',
},
{
name: 'type',
select: true,
value: 'primary',
types: [
'primary',
'secondary',
'danger',
'link',
'text'
]
},
{
name: 'size',
select: true,
value: 'none',
types: [
'none',
'small',
'mini'
]
},
{
name: 'block',
toggle: true,
value: false,
},
{
name: 'disabled',
toggle: true,
value: false,
},
{
name: 'icon',
select: true,
value: 'none',
types: [
'none',
'nucleus-circle-check',
'nucleus-cross'
]
},
{
name: 'iconSize',
select: true,
value: 'none',
types: [
'none',
'medium',
'small',
'mini'
]
},
{
name: 'iconOnly',
toggle: true,
value: false,
}
]
class Playground extends Component {
properties = PROPERTIES;

code = null;

init() {
super.init(...arguments);
registerComputedProperties(this, get(this, 'properties'));
this._generateCode();
}

@action
onchange(name, value) {
let properties = get(this, 'properties');
if (name === 'iconOnly' && !name.value) {
let changables = ['label', 'type', 'icon'];
let [
label,
type,
icon
] = properties.filter(prop => changables.includes(prop.name));

set(label, 'value', '');
set(type, 'value', 'text');
set(icon, 'value', (icon.value === 'none') ? icon.types[1] : icon.value);
shibulijack-fd marked this conversation as resolved.
Show resolved Hide resolved
}
handlePropertyChange(properties, name, value);
this._generateCode();
}


_generateCode() {
let attributes = get(this, 'properties').map((prop) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible to move this mapping part into util as well? Seems to be duplicated.

return {
name: prop.name,
value: prop.value,
}
});

set(this, 'code', generateCode({
component:'nucleus-button',
attributes,
multiline: true
}));
}
}

export default Playground;
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// BEGIN-SNIPPET nucleus-icon.js
import Component from '@ember/component';
import { inject } from '@ember/service';
import copyToClipboard from '../../utils/copy-to-clipboard';

const ICON_LIST = [
"nucleus-circle-check",
Expand All @@ -13,11 +15,20 @@ const ICON_LIST = [
];

export default Component.extend({
flashMessages: inject(),

icons: null,

init() {
this._super(...arguments);
this.set('icons', ICON_LIST)
},

actions: {
copyIcon(icon) {
copyToClipboard(icon)
.then(this.flashMessages.success(`Copied '${icon}' to clipboard`));
}
}
});
// END-SNIPPET
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import Component from '@ember/component';
import { get, set, action } from "@ember/object";
import {
registerComputedProperties,
handlePropertyChange,
generateCode
} from '../../utils/playground';

const PROPERTIES = [
{
name: 'size',
select: true,
value: 'large',
types: [
'small',
'medium',
'large'
]
},
{
name: 'variant',
select: true,
value: 'none',
types: [
'none',
'danger',
'success'
]
}
]

class Playground extends Component {
properties = PROPERTIES;

code = null;

init() {
super.init(...arguments);
registerComputedProperties(this, get(this, 'properties'));
this._generateCode();
}

@action
onchange(name, value) {
handlePropertyChange(get(this, 'properties'), name, value);
this._generateCode();
}


_generateCode() {
let props = get(this, 'properties').map((prop) => {
return {
name: prop.name,
value: prop.value,
}
});

let staticProps = [
{
name: 'name',
value: 'nucleus-circle-help'
}
];

set(this, 'code', generateCode({
component: 'nucleus-icon',
attributes: [...staticProps, ...props]
}));
}
}

export default Playground;

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import Component from '@ember/component';
import { get, set, action } from "@ember/object";
import {
registerComputedProperties,
handlePropertyChange,
generateCode
} from '../../utils/playground';

const PROPERTIES = [
{
name: 'type',
select: true,
value: 'warning',
types: [
'warning',
'danger',
'success',
'info'
]
},
{
name: 'title',
input: true,
value: 'This message has been identified as spam.',
},
{
name: 'isDismissible',
toggle: true,
value: true
}
]

class Playground extends Component {
properties = PROPERTIES;

code = null;

init() {
super.init(...arguments);
registerComputedProperties(this, get(this, 'properties'));
this._generateCode();
}

@action
onchange(name, value) {
handlePropertyChange(get(this, 'properties'), name, value);
this._generateCode();
}


_generateCode() {
let attributes = get(this, 'properties').map((prop) => {
return {
name: prop.name,
value: prop.value,
}
});

set(this, 'code', generateCode({
component: 'nucleus-inline-banner',
attributes,
multiline:true
}));
}
}

export default Playground;

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Component from '@ember/component';

export default Component.extend({

});
21 changes: 21 additions & 0 deletions packages/@nucleus/tests/dummy/app/components/playground/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Component from '@ember/component';
import { action, computed, get } from '@ember/object';
import { highlightCode } from 'ember-cli-addon-docs/utils/compile-markdown';
import copyToClipboard from '../../utils/copy-to-clipboard';


class Code extends Component {
@computed('code')
get computedCode() {
return highlightCode(get(this, 'code'), 'hbs');
}


@action
copyCode() {
copyToClipboard(get(this, 'code'))
.then(this.flashMessages.success(`Code copied.`));
}
}

export default Code;
Loading