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(text randomizer): implement text randomizer and toggle between wheel #14

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,28 @@ Look for files in

- `src/App.js`

## Patching dependencies

We are patching `wired-elements` and `wired-elements-react`, because some of the requirements that we sought weren't available in these 2 libraries.

To apply the patches under the [patches](./patches) folder, do this in the root of this project (this is also automatically executed after the `yarn` command):

```shell
yarn postinstall
```

To apply a new patch, go to the `node_modules` folder and find the package that you want to patch. Some tips when patching:

1. Remember that you need to patch the transpiled JavaScript files instead of TypeScript files.
2. Changing `node_modules` requires us to restart the development server, so the overall process might take some time, especially with trials and errors.
3. Remember to read the package's docs, package's GitHub issues, and other discussions (e.g. StackOverflow). They might save you some time.

After finishing your patch, let's say that you are patching something inside `wired-elements`, do this in the root of this project:

```shell
yarn patch-package wired-elements
```

## Contributing

If you would like to help us with this project you can learn about our initiative from [twitter](https://twitter.com/ans4175/status/1457313278015639553?s=20). Before you contribute to this project, please make sure to read our [CONTRIBUTING](CONTRIBUTING.md) file.
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"build": "CI=false && react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"prettify": "prettier --write src"
"prettify": "prettier --write src",
"postinstall": "patch-package"
Copy link
Owner

Choose a reason for hiding this comment

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

hoo baru tahu begini, berarti ini auto jalan?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

iyak, jadi urutannya:

yarn --> node_modules terinstall (raw) --> patch-package (apply diff dari patches yang ada)

},
"eslintConfig": {
"extends": [
Expand All @@ -44,6 +45,7 @@
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"patch-package": "6.4.7",
"prettier": "2.4.1"
}
}
260 changes: 260 additions & 0 deletions patches/wired-elements+3.0.0-rc.6.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
diff --git a/node_modules/wired-elements/lib/wired-item.d.ts b/node_modules/wired-elements/lib/wired-item.d.ts
index b2ee940..c6d4465 100644
--- a/node_modules/wired-elements/lib/wired-item.d.ts
+++ b/node_modules/wired-elements/lib/wired-item.d.ts
@@ -3,6 +3,7 @@ import { TemplateResult, CSSResultArray } from 'lit';
export declare class WiredItem extends WiredBase {
value: string;
name: string;
+ disabled: boolean;
selected: boolean;
static get styles(): CSSResultArray;
render(): TemplateResult;
diff --git a/node_modules/wired-elements/lib/wired-item.js b/node_modules/wired-elements/lib/wired-item.js
index e9c5d3c..a07b161 100644
--- a/node_modules/wired-elements/lib/wired-item.js
+++ b/node_modules/wired-elements/lib/wired-item.js
@@ -16,6 +16,7 @@ let WiredItem = class WiredItem extends WiredBase {
super(...arguments);
this.value = '';
this.name = '';
+ this.disabled = false;
this.selected = false;
}
static get styles() {
@@ -69,6 +70,12 @@ let WiredItem = class WiredItem extends WiredBase {
button:active span {
transform: scale(1.02);
}
+ button[disabled] {
+ opacity: 0.6 !important;
+ background: rgba(0, 0, 0, 0.07);
+ cursor: default;
+ pointer-events: none;
+ }
#overlay {
display: none;
}
@@ -91,7 +98,7 @@ let WiredItem = class WiredItem extends WiredBase {
}
render() {
return html `
- <button class="${this.selected ? 'selected' : ''}">
+ <button class="${this.selected ? 'selected' : ''}" ?disabled="${this.disabled}">
<div id="overlay"><svg></svg></div>
<span><slot></slot></span>
</button>`;
@@ -122,6 +129,10 @@ __decorate([
property({ type: Boolean }),
__metadata("design:type", Object)
], WiredItem.prototype, "selected", void 0);
+__decorate([
+ property({ type: Boolean }),
+ __metadata("design:type", Object)
+], WiredItem.prototype, "disabled", void 0);
WiredItem = __decorate([
customElement('wired-item')
], WiredItem);
diff --git a/node_modules/wired-elements/lib/wired-lib.js b/node_modules/wired-elements/lib/wired-lib.js
Copy link
Owner

Choose a reason for hiding this comment

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

cara bikinnya ini gimana ji? BIkin file perubahannya, terus di diff sendiri dan hasil diffnya masukkin sini?

Copy link
Owner

Choose a reason for hiding this comment

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

naming filenya juga?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

o ini ada caranya, maap, habis ini saya tambahin di READMEnya

index 5b1d153..9d3ec39 100644
--- a/node_modules/wired-elements/lib/wired-lib.js
+++ b/node_modules/wired-elements/lib/wired-lib.js
@@ -90,7 +90,7 @@ export function ellipse(parent, x, y, width, height, seed) {
}
export function hachureFill(points, seed) {
const hf = new ZigZagFiller(fillHelper);
- const ops = hf.fillPolygon(points, options(seed));
+ const ops = hf.fillPolygons([points], options(seed));
return createPathNode(ops, null);
}
export function hachureEllipseFill(cx, cy, width, height, seed) {
diff --git a/node_modules/wired-elements/lib/wired-tab.d.ts b/node_modules/wired-elements/lib/wired-tab.d.ts
index 0ba5fe7..45270d9 100644
--- a/node_modules/wired-elements/lib/wired-tab.d.ts
+++ b/node_modules/wired-elements/lib/wired-tab.d.ts
@@ -4,6 +4,8 @@ import './wired-item.js';
export declare class WiredTab extends WiredBase {
name: string;
label: string;
+ disabled?: boolean;
+ hasBorder?: boolean;
private resizeObserver?;
private windowResizeHandler?;
constructor();
diff --git a/node_modules/wired-elements/lib/wired-tab.js b/node_modules/wired-elements/lib/wired-tab.js
index 505dc90..ac69e4d 100644
--- a/node_modules/wired-elements/lib/wired-tab.js
+++ b/node_modules/wired-elements/lib/wired-tab.js
@@ -17,6 +17,8 @@ let WiredTab = class WiredTab extends WiredBase {
super();
this.name = '';
this.label = '';
+ this.disabled = false;
+ this.hasBorder = true;
if (window.ResizeObserver) {
this.resizeObserver = new window.ResizeObserver(() => {
if (this.svg) {
@@ -74,7 +76,9 @@ let WiredTab = class WiredTab extends WiredBase {
return [s.width, s.height];
}
draw(svg, s) {
- rectangle(svg, 2, 2, s[0] - 4, s[1] - 4, this.seed);
+ if (this.hasBorder) {
Copy link
Owner

Choose a reason for hiding this comment

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

haha nice

+ rectangle(svg, 2, 2, s[0] - 4, s[1] - 4, this.seed);
+ }
}
};
__decorate([
@@ -85,6 +89,14 @@ __decorate([
property({ type: String }),
__metadata("design:type", Object)
], WiredTab.prototype, "label", void 0);
+__decorate([
+ property({ type: Boolean }),
+ __metadata("design:type", Object)
+], WiredTab.prototype, "hasBorder", void 0);
+__decorate([
+ property({ type: Boolean }),
+ __metadata("design:type", Object)
+], WiredTab.prototype, "disabled", void 0);
WiredTab = __decorate([
customElement('wired-tab'),
__metadata("design:paramtypes", [])
diff --git a/node_modules/wired-elements/lib/wired-tabs.d.ts b/node_modules/wired-elements/lib/wired-tabs.d.ts
index cb2b68f..138f4a5 100644
--- a/node_modules/wired-elements/lib/wired-tabs.d.ts
+++ b/node_modules/wired-elements/lib/wired-tabs.d.ts
@@ -1,6 +1,7 @@
import { TemplateResult, CSSResultArray, LitElement } from 'lit';
export declare class WiredTabs extends LitElement {
selected?: string;
+ alignment?: string;
private slotElement?;
private pages;
private pageMap;
diff --git a/node_modules/wired-elements/lib/wired-tabs.js b/node_modules/wired-elements/lib/wired-tabs.js
index f9dfb28..aa4f88a 100644
--- a/node_modules/wired-elements/lib/wired-tabs.js
+++ b/node_modules/wired-elements/lib/wired-tabs.js
@@ -7,14 +7,17 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
-import { BaseCSS } from './wired-base';
+import { fireEvent, BaseCSS } from './wired-base';
import { css, html, LitElement } from 'lit';
import { customElement, property, query } from 'lit/decorators.js';
+import { classMap } from 'lit-html/directives/class-map';
+
let WiredTabs = class WiredTabs extends LitElement {
constructor() {
super(...arguments);
this.pages = [];
this.pageMap = new Map();
+ this.alignment = 'left';
}
static get styles() {
return [
@@ -39,15 +42,33 @@ let WiredTabs = class WiredTabs extends LitElement {
-webkit-flex-direction: row;
flex-direction: row;
}
+
+ .left {
+ justify-content: flex-start;
+ }
+ .center {
+ justify-content: center;
+ }
+ .right {
+ justify-content: flex-end;
+ }
`
];
}
+ onTabClick(page) {
+ if (page.__disabled) {
+ return;
+ }
+
+ this.selected = page.name;
+ this.fireSelected();
+ }
render() {
return html `
- <div id="bar">
+ <div id="bar" class=${classMap({ [this.alignment]: true })}>
${this.pages.map((p) => html `
- <wired-item role="tab" .value="${p.name}" .selected="${p.name === this.selected}" ?aria-selected="${p.name === this.selected}"
- @click="${() => this.selected = p.name}">${p.label || p.name}</wired-item>
+ <wired-item role="tab" .disabled="${p.__disabled}" .value="${p.name}" .selected="${p.name === this.selected}" ?aria-selected="${p.name === this.selected}"
+ @click="${() => this.onTabClick(p)}">${p.label || p.name}</wired-item>
`)}
</div>
<div>
@@ -66,7 +87,10 @@ let WiredTabs = class WiredTabs extends LitElement {
if (n.nodeType === Node.ELEMENT_NODE && n.tagName.toLowerCase() === 'wired-tab') {
const e = n;
this.pages.push(e);
- const name = e.getAttribute('name') || '';
+ // PATCHNOTE(imballinst): I don't know what's going on, but
+ // there is no `name` attribute, nor is `__name` (in the DOM),
+ // but we can access `__name` directly.
+ const name = e.__name || '';
if (name) {
name.trim().split(' ').forEach((nameSegment) => {
if (nameSegment) {
@@ -105,6 +129,11 @@ let WiredTabs = class WiredTabs extends LitElement {
}
updated() {
const newPage = this.getElement();
+
+ if (newPage.__disabled) {
+ return;
+ }
+
for (let i = 0; i < this.pages.length; i++) {
const p = this.pages[i];
if (p === newPage) {
@@ -148,7 +177,13 @@ let WiredTabs = class WiredTabs extends LitElement {
else {
index--;
}
+
+ if (list[index].__disabled) {
+ return;
+ }
+
this.selected = list[index].name || '';
+ this.fireSelected();
}
}
selectNext() {
@@ -170,14 +205,27 @@ let WiredTabs = class WiredTabs extends LitElement {
else {
index++;
}
+
+ if (list[index].__disabled) {
+ return;
+ }
+
this.selected = list[index].name || '';
+ this.fireSelected();
}
}
+ fireSelected() {
+ fireEvent(this, 'selected', { selected: this.selected });
+ }
Comment on lines +246 to +248
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ans-4175 begini kurang lebih cara saya nambahin event emitter (?) di komponennya

Copy link
Owner

Choose a reason for hiding this comment

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

okay, berarti nanti dibacanya di atasnya onselected sama seperti yang radio group button

Copy link
Contributor Author

Choose a reason for hiding this comment

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

iyak, betul

};
__decorate([
property({ type: String }),
__metadata("design:type", String)
], WiredTabs.prototype, "selected", void 0);
+__decorate([
+ property({ type: String }),
+ __metadata("design:type", String)
+], WiredTabs.prototype, "alignment", void 0);
__decorate([
query('slot'),
__metadata("design:type", HTMLSlotElement)
47 changes: 47 additions & 0 deletions patches/wired-elements-react+0.1.5.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
diff --git a/node_modules/wired-elements-react/lib/WiredItem.d.ts b/node_modules/wired-elements-react/lib/WiredItem.d.ts
index 65b8bcd..2bf7b3e 100644
--- a/node_modules/wired-elements-react/lib/WiredItem.d.ts
+++ b/node_modules/wired-elements-react/lib/WiredItem.d.ts
@@ -1,5 +1,6 @@
import * as React from 'react';
import { WiredItem as _WiredItem } from 'wired-elements/lib/wired-item.js';
export declare const WiredItem: React.ForwardRefExoticComponent<Partial<_WiredItem> & {} & {
+ disabled?: boolean;
children?: React.ReactNode;
} & React.RefAttributes<unknown>>;
diff --git a/node_modules/wired-elements-react/lib/WiredTab.d.ts b/node_modules/wired-elements-react/lib/WiredTab.d.ts
index 9c3d248..d1e1ab9 100644
--- a/node_modules/wired-elements-react/lib/WiredTab.d.ts
+++ b/node_modules/wired-elements-react/lib/WiredTab.d.ts
@@ -2,4 +2,5 @@ import * as React from 'react';
import { WiredTab as CE } from 'wired-elements/lib/wired-tab.js';
export declare const WiredTab: React.ForwardRefExoticComponent<Partial<CE> & {} & {
children?: React.ReactNode;
+ disabled?: boolean;
} & React.RefAttributes<unknown>>;
diff --git a/node_modules/wired-elements-react/lib/WiredTabs.d.ts b/node_modules/wired-elements-react/lib/WiredTabs.d.ts
index cbafd3d..3299a2a 100644
--- a/node_modules/wired-elements-react/lib/WiredTabs.d.ts
+++ b/node_modules/wired-elements-react/lib/WiredTabs.d.ts
@@ -1,5 +1,8 @@
import * as React from 'react';
import { WiredTabs as CE } from 'wired-elements/lib/wired-tabs.js';
-export declare const WiredTabs: React.ForwardRefExoticComponent<Partial<CE> & {} & {
+export declare const WiredTabs: React.ForwardRefExoticComponent<Partial<CE> & {
+ onselected?: ((e: Event) => unknown) | undefined;
+} & {
+ alignment?: string;
children?: React.ReactNode;
} & React.RefAttributes<unknown>>;
diff --git a/node_modules/wired-elements-react/lib/WiredTabs.js b/node_modules/wired-elements-react/lib/WiredTabs.js
index 6c93459..461610a 100644
--- a/node_modules/wired-elements-react/lib/WiredTabs.js
+++ b/node_modules/wired-elements-react/lib/WiredTabs.js
@@ -1,4 +1,6 @@
import * as React from 'react';
import { createComponent } from '@lit-labs/react';
import { WiredTabs as CE } from 'wired-elements/lib/wired-tabs.js';
-export const WiredTabs = createComponent(React, 'wired-tabs', CE);
+export const WiredTabs = createComponent(React, 'wired-tabs', CE, {
+ onselected: 'selected'
+});
16 changes: 15 additions & 1 deletion src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ wired-card {
}

section {
max-width: 400px;
/* Make this the same with the wheel's width. */
/* That way, the wheel will be properly centered. */
max-width: 445px;
margin: 0 auto;
padding: 10px 0;
}
Expand Down Expand Up @@ -121,6 +123,18 @@ wired-input {
font-size: 0.6em;
}

/* Randomizer stuff. */
.randomizer-text-container {
margin-top: 24px;
}

.randomizer-text-helper {
margin-top: 8px;
margin-bottom: 8px;
color: rgba(0, 0, 0, 0.65);
}

/* Result modal stuff. */
.result-modal-section {
max-height: 100vh;
padding: 0;
Expand Down
Loading