Skip to content

Commit

Permalink
add types file
Browse files Browse the repository at this point in the history
  • Loading branch information
nichoth committed Jun 5, 2024
1 parent 32b6f8f commit e2a56dd
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 6 deletions.
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
"test": "npm run build && npm run lint && esbuild --bundle test/index.js | tape-run",
"ci:test:tape-run": "esbuild --bundle test/index.js | tape-run",
"test:open": "npm run build && esbuild --bundle test/index.js | tape-run --browser chrome --keep-open",
"build:main": "esbuild src/index.js --define:VERSION=\\\"$npm_package_version\\\" --outfile=dist/index.js",
"build:main": "esbuild src/index.js --define:VERSION=\\\"$npm_package_version\\\" --outfile=dist/tonic.js",
"build:minify": "esbuild src/index.js --keep-names --minify --outfile=dist/tonic.min.js",
"build:types": "npx tsc -p ./jsconfig.build.json",
"build": "mkdir -p ./dist && rm -rf ./dist/* && npm run build:main && npm run build:minify && npm run build:types",
"build": "mkdir -p ./dist && rm -rf ./dist/* && npm run build:main && npm run build:minify && cp ./src/index.d.ts ./dist/tonic.d.ts",
"preversion": "npm run lint",
"version": "auto-changelog -p --template keepachangelog auto-changelog --breaking-pattern 'BREAKING CHANGE:' && git add CHANGELOG.md",
"postversion": "git push --follow-tags && npm publish",
Expand All @@ -27,7 +26,6 @@
"eslint-config-standard": "^17.1.0",
"tap-arc": "1.2.2",
"tape-run": "^11.0.0",
"typescript": "5.4.4",
"uuid": "^9.0.0"
},
"exports": {
Expand Down
117 changes: 117 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/**
* An instance of a Tonic element
* @typedef {
* (TonicComponent extends Tonic) |
* ()=>TonicTemplate|Promise<TonicTemplate>
* } TonicComponent
*/

interface ITonicComponent extends Tonic {
}

type TonicComponent<T = {}> = ITonicComponent | ((T)=>TonicTemplate|Element)

/**
* Class Tonic
*/
export class Tonic<T = {}> extends HTMLElement {
static _tags: string;
static _refIds: any[];
static _data: {};
static _states: {};
static _children: {};
static _reg: {};
static _stylesheetRegistry: any[];
static _index: number;
static get version(): any;
static get SPREAD(): RegExp;
static get ESC(): RegExp;
static get AsyncFunctionGenerator(): Function;
static get AsyncFunction(): Function;
static get MAP(): {
'"': string;
'&': string;
'\'': string;
'<': string;
'>': string;
'`': string;
'/': string;
};
static ssr: any;
static nonce: any;
static _createId(): string;
static _normalizeAttrs(o: any, x?: {}): {};
static match(el: any, s: any): any;
static getTagName(camelName: any): any;
static getPropertyNames(proto: any): string[];
/**
* Add a component
* @param {TonicComponent} c
* @param {string} [htmlName] Name of the element, default to the class name
* @returns {void}
*/
static add(c: TonicComponent, htmlName?: string): void;
static registerStyles(stylesheetFn: any): void;
static escape(s: any): any;
static unsafeRawString(s: any, templateStrings: any): TonicTemplate;
/**
* @type {Element[] & { __children__? }}
* @private
*/
private elements;
/**
* @type {ChildNode[] & { __children__? }}
* @private
*/
private nodes;
_props: any; /** @private */
_state: any;
preventRenderOnReconnect: boolean;
props:T;
/**
* Render the component.
* @abstract
* @return {TonicTemplate|Promise<TonicTemplate>}
*/
render(): TonicTemplate | Promise<TonicTemplate>;
get isTonicComponent(): boolean;
_checkId(): string;
set state(newState: any);
get state(): any;
_events(): void;
_prop(o: any): string;
_placehold(r: any): string;
dispatch(eventName: any, detail?: any): void;
html(strings: any, ...values: any[]): TonicTemplate;
scheduleReRender(oldProps: any): any;
pendingReRender: any;
reRender(o?: {}): any;
handleEvent(e: any): void;
_drainIterator(target: any, iterator: any): any;
/**
* _set
* @param {Element|InstanceType<typeof Tonic>|ShadowRoot} target
* @param {()=>any} render
* @param {string} content
* @returns {Promise<void>}
* @private
*/
private _set;
_apply(target: any, content: any): void;
connectedCallback(): Promise<any>;
root: any;
_id: any;
_source: string;
isInDocument(target: any): boolean;
disconnectedCallback(): void;
}
export default Tonic;
declare class TonicTemplate {
constructor(rawText: any, templateStrings: any, unsafe: any);
isTonicTemplate: boolean;
unsafe: any;
rawText: any;
templateStrings: any;
valueOf(): any;
toString(): any;
}
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class TonicTemplate {

/**
* Class Tonic
* @template {T = {}} T Type for the props
*/
export class Tonic extends window.HTMLElement {
static _tags = ''
Expand Down Expand Up @@ -61,7 +62,7 @@ export class Tonic extends window.HTMLElement {
delete Tonic._states[super.id]
this._state = state || {}
this.preventRenderOnReconnect = false
this.props = {}
this.props = {} /** @type {T} */
this.elements = [...this.children]
this.elements.__children__ = true
this.nodes = [...this.childNodes]
Expand Down
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test } from '@bicycle-codes/tapzero'
import { v4 as uuid } from 'uuid'
import Tonic from '../dist/index.js'
import Tonic from '../dist/tonic.js'

const sleep = async t => new Promise(resolve => setTimeout(resolve, t))

Expand Down

0 comments on commit e2a56dd

Please sign in to comment.