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

Add typescript friendly mixins #85

Merged
merged 3 commits into from
May 30, 2024
Merged
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
5 changes: 5 additions & 0 deletions lib/animated-routing.mixin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import animatedRouteMixin from './animated-routing-mixin.js';
function animatedRoutingMixin(Superclass, className) {
return animatedRouteMixin(Superclass, className);
}
export default animatedRoutingMixin;
9 changes: 9 additions & 0 deletions lib/animated-routing.mixin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {type LitElement} from 'lit';
import {type Constructor, type RoutingMixinInterface} from './routing.mixin.js';
import animatedRouteMixin from './animated-routing-mixin.js';

function animatedRoutingMixin<T extends Constructor<LitElement>>(Superclass:T, className:string) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be specific to LitElement?

Suggested change
function animatedRoutingMixin<T extends Constructor<LitElement>>(Superclass:T, className:string) {
function animatedRoutingMixin<T extends Constructor<HTMLElement>>(Superclass:T, className:string) {

Copy link
Contributor Author

@jrobinson01 jrobinson01 May 24, 2024

Choose a reason for hiding this comment

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

I would think so, but there are some downstream (platform-tools) components that extend this mixin that complain when using LitElement HTMLElement. I know it's not ideal to be restricted to LitElement, but I don't believe that we'll be using any other library for the foreseeable future.

Choose a reason for hiding this comment

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

i commented below but without the LitElement as the extended class, it causes typescript errors when using the lifecycle methods. @barronhagerman @jrobinson01

return animatedRouteMixin(Superclass, className) as unknown as Constructor<RoutingMixinInterface> & T;
}

export default animatedRoutingMixin;
7 changes: 7 additions & 0 deletions lib/routing.mixin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import BasicRoutingInterface from './routing-interface.js';
import routingMixin from './routing-mixin.js';
const RoutingMixin = (superclass) => {
return routingMixin(superclass);
};
//exporting BasicRoutingInterface for backward compatibility - don't break consumer imports
export { RoutingMixin, BasicRoutingInterface };
16 changes: 16 additions & 0 deletions lib/routing.mixin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import BasicRoutingInterface from './routing-interface.js';
import type { Context } from './page.js';
import type RouteTreeNode from './route-tree-node.js';
import type { LitElement } from 'lit';
import routingMixin from './routing-mixin.js';
export type Constructor<T = {}> = new (...args: any[]) => T;
export declare class RoutingMixinInterface {
routeEnter(currentNode: RouteTreeNode, nextNodeIfExists: RouteTreeNode | undefined, routeId: string, context: Context): Promise<boolean | void>;
routeExit(currentNode: RouteTreeNode, nextNode: RouteTreeNode | undefined, routeId: string, context: Context): Promise<boolean | void>;
}
const RoutingMixin = <T extends Constructor<LitElement>>(superclass: T) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here. Should this be specific to LitElement?

Suggested change
const RoutingMixin = <T extends Constructor<LitElement>>(superclass: T) => {
const RoutingMixin = <T extends Constructor<HTMLElement>>(superclass: T) => {

Choose a reason for hiding this comment

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

This was something we discovered in origin. If we don't use LitElement as the super class we get linter errors when using the lifecycle methods like connectedCallback etc

https://lit.dev/docs/composition/mixins/#mixins-in-typescript

return routingMixin(superclass) as unknown as Constructor<RoutingMixinInterface> & T;
};

//exporting BasicRoutingInterface for backward compatibility - don't break consumer imports
export { RoutingMixin, BasicRoutingInterface };
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
"files": [
"lib/",
"*.js",
"*.mixin.ts",
"*.d.ts",
"package.json",
"README.md"
],
"scripts": {
"build": "tsc",
"clean": "rimraf './lib/**/*.d.ts' && rimraf './*.d.ts'",
"build": "tsc && yarn build:ts",
"build:ts": "tsc --project tsconfig.buildts.json",
"clean": "rimraf './lib/**/*.d.ts' && rimraf './*.d.ts' && rimraf './lib/*.mixin.js'",
"prepublish": "yarn clean && yarn build",
"test": "karma start test/karma.config.cjs --single-run"
},
Expand All @@ -40,9 +42,13 @@
"@polymer/polymer": "^3.4.1",
"jasmine-core": "^5.x",
"karma": "^6.x",
"lit": "^3.1.3",
"karma-chrome-launcher": "^3.2.0",
"karma-jasmine": "5.x",
"karma-spec-reporter": "^0.0.36",
"typescript": "^5.2.2"
},
"peerDependencies": {
"lit":"^3.1.3"
}
}
9 changes: 9 additions & 0 deletions tsconfig.buildts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

{
"extends": "./tsconfig.json",
"compilerOptions": {
"noEmit": false,
"emitDeclarationOnly": false,
},
"include": ["lib/routing.mixin.ts", "lib/animated-routing.mixin.ts"],
}
42 changes: 42 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
resolved "https://registry.yarnpkg.com/@colors/colors/-/colors-1.5.0.tgz#bb504579c1cae923e6576a4f5da43d25f97bdbd9"
integrity sha512-ooWCrlZP11i8GImSjTHYHLkvFDP48nS4+204nGb1RiX/WXYHmJA2III9/e2DWVabCESdW7hBAEzHRqUn9OUVvQ==

"@lit-labs/ssr-dom-shim@^1.2.0":
version "1.2.0"
resolved "https://registry.yarnpkg.com/@lit-labs/ssr-dom-shim/-/ssr-dom-shim-1.2.0.tgz#353ce4a76c83fadec272ea5674ede767650762fd"
integrity sha512-yWJKmpGE6lUURKAaIltoPIE/wrbY3TEkqQt+X0m+7fQNnAv0keydnYvbiJFP1PnMhizmIWRWOG5KLhYyc/xl+g==

"@lit/reactive-element@^2.0.4":
version "2.0.4"
resolved "https://registry.yarnpkg.com/@lit/reactive-element/-/reactive-element-2.0.4.tgz#8f2ed950a848016383894a26180ff06c56ae001b"
integrity sha512-GFn91inaUa2oHLak8awSIigYz0cU0Payr1rcFsrkf5OJ5eSPxElyZfKh0f2p9FsTiZWXQdWGJeXZICEfXXYSXQ==
dependencies:
"@lit-labs/ssr-dom-shim" "^1.2.0"

"@polymer/polymer@^3.4.1":
version "3.5.1"
resolved "https://registry.yarnpkg.com/@polymer/polymer/-/polymer-3.5.1.tgz#4b5234e43b8876441022bcb91313ab3c4a29f0c8"
Expand Down Expand Up @@ -41,6 +53,11 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.11.18.tgz#8dfb97f0da23c2293e554c5a50d61ef134d7697f"
integrity sha512-DHQpWGjyQKSHj3ebjFI/wRKcqQcdR+MoFBygntYOZytCqNfkd2ZC4ARDJ2DQqhjH5p85Nnd3jhUJIXrszFX/JA==

"@types/trusted-types@^2.0.2":
version "2.0.7"
resolved "https://registry.yarnpkg.com/@types/trusted-types/-/trusted-types-2.0.7.tgz#baccb07a970b91707df3a3e8ba6896c57ead2d11"
integrity sha512-ScaPdn1dQczgbl0QFTeTOmVHFULt394XJgOQNoyVhZ6r2vLnMLJfBPd53SB52T/3G36VI1/g2MZaX0cwDuXsfw==

"@webcomponents/shadycss@^1.9.1":
version "1.10.0"
resolved "https://registry.yarnpkg.com/@webcomponents/shadycss/-/shadycss-1.10.0.tgz#7a80ec1e8b271fb3f0cc02cd4358b877a303545d"
Expand Down Expand Up @@ -577,6 +594,31 @@ karma@^6.x:
ua-parser-js "^0.7.30"
yargs "^16.1.1"

lit-element@^4.0.4:
version "4.0.5"
resolved "https://registry.yarnpkg.com/lit-element/-/lit-element-4.0.5.tgz#f20cd8a6231eaf5358f7a6877ca6ea7628fa2015"
integrity sha512-iTWskWZEtn9SyEf4aBG6rKT8GABZMrTWop1+jopsEOgEcugcXJGKuX5bEbkq9qfzY+XB4MAgCaSPwnNpdsNQ3Q==
dependencies:
"@lit-labs/ssr-dom-shim" "^1.2.0"
"@lit/reactive-element" "^2.0.4"
lit-html "^3.1.2"

lit-html@^3.1.2:
version "3.1.3"
resolved "https://registry.yarnpkg.com/lit-html/-/lit-html-3.1.3.tgz#ae2e9fee0258d0a1b5d7b86c87da51117e4f911b"
integrity sha512-FwIbqDD8O/8lM4vUZ4KvQZjPPNx7V1VhT7vmRB8RBAO0AU6wuTVdoXiu2CivVjEGdugvcbPNBLtPE1y0ifplHA==
dependencies:
"@types/trusted-types" "^2.0.2"

lit@^3.1.3:
version "3.1.3"
resolved "https://registry.yarnpkg.com/lit/-/lit-3.1.3.tgz#809ecdaccfea47e1e3b46649fae6c6e7b9802675"
integrity sha512-l4slfspEsnCcHVRTvaP7YnkTZEZggNFywLEIhQaGhYDczG+tu/vlgm/KaWIEjIp+ZyV20r2JnZctMb8LeLCG7Q==
dependencies:
"@lit/reactive-element" "^2.0.4"
lit-element "^4.0.4"
lit-html "^3.1.2"

lodash@^4.17.21:
version "4.17.21"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
Expand Down
Loading