Skip to content

Commit

Permalink
chore: rebase with reorg branch
Browse files Browse the repository at this point in the history
  • Loading branch information
wainola committed Jan 22, 2024
2 parents 52d26af + 18c3ac1 commit 08146cd
Show file tree
Hide file tree
Showing 37 changed files with 1,952 additions and 1,552 deletions.
14 changes: 11 additions & 3 deletions packages/widget/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@
"name": "@buildwithsygma/sygmaprotocol-widget",
"description": "Sygma Protocol Widget",
"version": "0.1.0",
"main": "build/index.js",
"module": "build/index.js",
"main": "build/sygmaprotocol-widget.js",
"module": "build/sygmaprotocol-widget.js",
"types": "build/index.d.ts",
"license": "LGPL-3.0-or-later",
"type": "module",
"files": [
"build",
"src"
],
"scripts": {
"start": "yarn run dev",
"dev": "vite",
"build": "yarn run lint:types && yarn run clean && vite build",
"serve": "vite preview",
"clean": "rm -rf ./build",
"lint": "yarn run lint:types && yarn run lint:code && yarn run lint:lit",
"lint:types": "tsc -p ./tsconfig.build.json --noEmit",
"lint:types": "tsc -p ./tsconfig.json --noEmit",
"lint:code": "eslint '{src,tests}/**/*.ts'",
"lint:lit": "lit-analyzer",
"test": "yarn run test:unit",
Expand All @@ -35,6 +40,8 @@
"lit": "3.0.0"
},
"devDependencies": {
"@open-wc/semantic-dom-diff": "^0.20.1",
"@open-wc/testing-helpers": "^3.0.0",
"eslint": "^8.48.0",
"eslint-plugin-lit": "^1.9.1",
"jsdom": "^23.2.0",
Expand All @@ -43,6 +50,7 @@
"typescript": "5.2.2",
"vite": "4.4.11",
"vite-plugin-clean": "^1.0.0",
"vite-plugin-dts": "^3.7.1",
"vitest": "^1.2.1"
}
}
127 changes: 69 additions & 58 deletions packages/widget/src/components/amount-selector/amount-selector.ts
Original file line number Diff line number Diff line change
@@ -1,113 +1,124 @@
import type { Resource } from '@buildwithsygma/sygma-sdk-core';
import type { HTMLTemplateResult } from 'lit';
import { LitElement, html } from 'lit';
import { customElement, property, state } from 'lit/decorators.js';
import { customElement, property, query } from 'lit/decorators.js';
import { ifDefined } from 'lit/directives/if-defined.js';
import type { Ref } from 'lit/directives/ref.js';
import { createRef, ref } from 'lit/directives/ref.js';
import { map } from 'lit/directives/map.js';
import { when } from 'lit/directives/when.js';
import { styles } from './styles';

@customElement('amount-selector')
export default class AmountSelector extends LitElement {
@customElement('sygma-resource-selector')
export class AmountSelector extends LitElement {
static styles = styles;
@property({
type: Array,
hasChanged: (n, o) => n !== o
})
resources?: Resource[];
resources: Resource[] = [];

@property({
type: Boolean
})
disabled = false;

@property({
type: Boolean,
hasChanged: (n, o) => n !== o
})
isNativeToken?: boolean;

@state({
hasChanged: (n, o) => n !== o
type: String
})
selectedNetworkChainId?: number;
accountBalance?: string;

@property({
type: String
})
tokenBalance?: string;
preselectedToken?: string;

@property({
type: String
type: Number
})
tokenName?: string;
preselectedAmount?: number;

@state({
hasChanged: (n, o) => n !== o
@property({
attribute: false
})
selectedAmount?: number;

inputRef: Ref<HTMLInputElement> = createRef();
onResourceSelected?: (resource: Resource) => void;

handleAmountChange = (event: Event): void => {
const { value } = event.target as HTMLInputElement;

this.selectedAmount = Number.parseFloat(value);
@property({
attribute: false
})
onAmountChange?: (amount: number) => void;

dispatchEvent(
new CustomEvent('amount-selector-change', {
detail: value,
bubbles: true,
composed: true
})
);
};
@query('.amountSelectorInput', true)
_input!: HTMLInputElement;

useMaxBalance = (): void => {
this.selectedAmount = Number.parseFloat(this.tokenBalance!);
this.preselectedAmount = Number.parseFloat(this.accountBalance!);
this._onInputAmountChange();
};

(this.inputRef.value as HTMLInputElement).value = `${this.selectedAmount}`;
const event = new Event('input', {
bubbles: true
});
_onInputAmountChange = (): void => {
const amount = Number.parseFloat(this._input.value);
this.onAmountChange?.(amount);
};

this.inputRef.value?.dispatchEvent(event);
_onResourceSelected = (event: Event): void => {
const { value } = event.target as HTMLOptionElement;
const resource = this.resources.find((n) => String(n.resourceId) == value);
if (resource) {
this.onResourceSelected?.(resource);
}
};

renderBalance(): HTMLTemplateResult {
return html`
<section class="balanceContent">
<span>${`${Number.parseFloat(this.tokenBalance!).toFixed(4)}`}</span>
<span>${`${Number.parseFloat(this.accountBalance!).toFixed(4)}`}</span>
<button class="maxButton" @click=${this.useMaxBalance}>Max</button>
</section>
`;
}

renderEntries(): Generator<unknown, void> | HTMLTemplateResult {
if (this.resources) {
return map(this.resources, (entry: Resource) => {
// TODO: render resource/token icon
return html`<option value=${entry.resourceId}>${entry.symbol}</option>`;
});
}
return html`<option selected value="">Token</option>`;
}

render(): HTMLTemplateResult {
return html`
<div class="amountSelectorContainer">
<section class="tokenBalanceSection">
<label class="amountSelectorLabel">Amount to transfer</label>
${when(this.tokenBalance, () => this.renderBalance())}
<section class="tokenBalanceSection">
<label class="amountSelectorLabel">Amount to transfer</label>
${when(this.accountBalance, () => this.renderBalance())}
</section>
<section class="amountSelectorSection">
<input
type="text"
class="amountSelectorInput"
placeholder="0"
@input=${this.handleAmountChange}
value=${ifDefined(this.selectedAmount)}
${ref(this.inputRef)}
/>
<base-selector
.entries=${this.resources}
.typeSelector=${'token'}
.disabled=${this.disabled}
></base-selector>
<input
type="text"
class="amountSelectorInput"
placeholder="0.000"
@change=${this._onInputAmountChange}
value=${ifDefined(this.preselectedAmount)}
/>
<section class="selectorSection">
<select
@change=${this._onResourceSelected}
?disabled=${this.disabled}
class="selector amountSelectorInput"
>
<option value="-1">-</option>
${this.renderEntries()}
</select>
</section>
</div>
</section>
</div>
`;
}
}

declare global {
interface HTMLElementTagNameMap {
'sygma-resource-selector': AmountSelector;
}
}
2 changes: 1 addition & 1 deletion packages/widget/src/components/amount-selector/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { default } from './amount-selector';
export { AmountSelector } from './amount-selector';
28 changes: 8 additions & 20 deletions packages/widget/src/components/amount-selector/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,65 +6,53 @@ export const styles = css`
padding: 4px 12px;
align-items: center;
border-radius: 24px;
width: 314px;
height: 116px;
width: 314px; /* TODO: remove hardcoded values */
height: 116px; /* TODO: ↑ */
flex-direction: column;
}
.amountSelectorLabel {
display: flex;
width: 100%;
flex-direction: row;
justify-content: flex-start;
color: var(--neutral-600, #525252);
font-family: Inter;
color: #525252;
font-size: 14px;
font-style: normal;
font-weight: 500;
line-height: 20px; /* 142.857% */
}
.amountSelectorSection {
display: flex;
width: 100%;
flex-direction: row;
justify-content: space-between;
align-items: center;
}
.amountSelectorInput {
border: none;
color: var(--neutral-600, #525252);
font-family: Inter;
color: #525252;
font-size: 34px;
font-style: normal;
font-weight: 500;
line-height: 40px; /* 117.647% */
line-height: 40px;
letter-spacing: -0.68px;
width: 137px;
width: 137px; /* TODO: remove hardcoded values */
}
.tokenBalanceSection {
display: flex;
flex-direction: row;
align-self: flex-start;
margin-top: 8px;
width: 100%;
}
.balanceContent {
display: flex;
flex-direction: row;
width: 100%;
justify-content: flex-end;
}
.balanceContent > span {
margin-right: 6px;
gap: 6px;
}
.maxButton {
color: var(--blue-600, #2563eb);
color: #2563eb;
border: none;
background: none;
font-weight: 500;
Expand Down
Loading

0 comments on commit 08146cd

Please sign in to comment.