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: export type declarations and add type linting #155

Merged
merged 1 commit into from
Mar 27, 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
39 changes: 34 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 15 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,31 @@
"type": "module",
"main": "dist/index.js",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
".": {
"default": "./dist/index.js",
"types": "./dist/index.d.ts"
},
"./*": "./dist/*"
},
"repository": {
"type": "git",
"url": "https://github.com/statistikzh/leu.git"
},
"homepage": "https://github.com/statistikzh/leu/",
"scripts": {
"analyze": "cem analyze --config custom-elements-manifest.config.js",
"build": "rimraf dist && npm run build:js && npm run build:css",
"build": "rimraf dist && npm run build:js && npm run build:types && npm run build:css",
"build:js": "rollup -c rollup.config.js",
"build:types": "tsc -p tsconfig.build.json",
"build:css": "postcss src/styles/theme.css -o dist/theme.css && cp dist/theme.css .storybook/static/",
"watch:css": "nodemon --watch 'src/styles/*' -e css --exec npm run build:css",
"lint": "npm run lint:eslint && npm run lint:prettier",
"lint:eslint": "eslint --ext .js,.mjs,.html . --ignore-path .gitignore ",
"lint:prettier": "prettier \"**/*.{js,mjs,md,html,json}\" --check --ignore-path .gitignore",
"lint:stylelint": "stylelint \"**/*.{js,css}\" --ignore-path .gitignore",
"lint:types": "tsc",
"format": "npm run format:eslint && npm run format:prettier",
"format:eslint": "eslint --ext .js,.mjs,.html . --fix --ignore-path .gitignore",
"format:prettier": "prettier \"**/*.{js,mjs,md,html,json}\" --write --ignore-path .gitignore",
Expand All @@ -46,6 +56,7 @@
"@rollup/plugin-babel": "^6.0.4",
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-typescript": "^11.1.6",
"@storybook/addon-designs": "^7.0.9",
"@storybook/addon-essentials": "^7.6.17",
"@storybook/addon-links": "^7.6.17",
Expand Down Expand Up @@ -85,7 +96,9 @@
"sinon": "^17.0.1",
"storybook": "^7.6.17",
"stylelint": "^15.10.3",
"stylelint-config-standard": "^34.0.0"
"stylelint-config-standard": "^34.0.0",
"tslib": "^2.6.2",
"typescript": "^5.4.3"
},
"customElements": "custom-elements.json",
"prettier": {
Expand Down
6 changes: 5 additions & 1 deletion src/components/button-group/ButtonGroup.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { html, LitElement } from "lit"
// @ts-ignore
import styles from "./button-group.css"

/**
Expand All @@ -17,7 +18,7 @@ export class LeuButtonGroup extends LitElement {
}

/**
* @param {HTMLElement} button
* @param {import("../button/Button").LeuButton} button
* @returns {string}
*/
static getButtonValue(button) {
Expand Down Expand Up @@ -71,6 +72,9 @@ export class LeuButtonGroup extends LitElement {
})
}

/**
* @param {import("../button/Button").LeuButton} button
*/
_handleButtonClick(button) {
if (!button.active) {
this.value = LeuButtonGroup.getButtonValue(button)
Expand Down
26 changes: 22 additions & 4 deletions src/components/select/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import "../menu/leu-menu-item.js"
import "../input/leu-input.js"
import "../popup/leu-popup.js"

// @ts-ignore
import styles from "./select.css"

/**
Expand All @@ -25,8 +26,7 @@ export class LeuSelect extends LitElement {

static get properties() {
return {
open: { type: Boolean, attribute: "open" },

open: { type: Boolean, reflect: true },
label: { type: String, reflect: true },
options: { type: Array },
value: { type: Array },
Expand All @@ -53,9 +53,14 @@ export class LeuSelect extends LitElement {
constructor() {
super()
this.open = false
this.disabled = false
this.open = false
this.multiple = false
this.clearable = false
this.filterable = false
this.value = []
this.options = []
this.label = ""

/** @internal */
this._arrowIcon = Icon("angleDropDown")
Expand All @@ -69,8 +74,17 @@ export class LeuSelect extends LitElement {
/** @internal */
this.deferedChangeEvent = false

/**
* @type {import("lit/directives/ref").Ref<import("../menu/Menu").LeuMenu>}
*/
this.menuRef = createRef()
/**
* @type {import("lit/directives/ref").Ref<import("../input/Input").LeuInput>}
*/
this.optionFilterRef = createRef()
/**
* @type {import("lit/directives/ref").Ref<HTMLButtonElement>}
*/
this.toggleButtonRef = createRef()
}

Expand Down Expand Up @@ -102,7 +116,11 @@ export class LeuSelect extends LitElement {
* @param {MouseEvent} event
*/
handleDocumentClick = (event) => {
if (!this.contains(event.target) && this.open) {
if (
event.target instanceof Node &&
!this.contains(event.target) &&
this.open
) {
this.closeDropdown()
}
}
Expand Down Expand Up @@ -320,7 +338,7 @@ export class LeuSelect extends LitElement {
<span class="label" id="select-label">${this.label}</span>
<span class="value"> ${this.getDisplayValue(this.value)} </span>
<span class="arrow-icon"> ${this._arrowIcon} </span>
${this.clearable && this.value !== "" && this.value.length !== 0
${this.clearable && this.value.length !== 0
? html`<button
type="button"
class="clear-button"
Expand Down
2 changes: 1 addition & 1 deletion src/lib/defineElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Adds a definition for a custom element to the custom element
* registry if it isn't defined before. Prefixes the name with `leu-`.
* @param {string} name
* @param {HTMLElement} constructor
* @param {CustomElementConstructor} constructor
*/
export function defineElement(name, constructor) {
if (!customElements.get(`leu-${name}`)) {
Expand Down
8 changes: 5 additions & 3 deletions src/lib/hasSlotController.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

/**
* A reactive controller that determines when slots exist.
* @implements { import("lit").ReactiveController }
* @typedef {import("lit").ReactiveController} ReactiveController
* @implements {ReactiveController}
*/
export class HasSlotController {
constructor(host, slotNames) {
Expand Down Expand Up @@ -76,8 +77,9 @@ export class HasSlotController {
const slot = event.target

if (
(this.slotNames.includes("[default]") && !slot.name) ||
(slot.name && this.slotNames.includes(slot.name))
slot instanceof HTMLSlotElement &&
((this.slotNames.includes("[default]") && !slot.name) ||
(slot.name && this.slotNames.includes(slot.name)))
) {
this.host.requestUpdate()
}
Expand Down
21 changes: 21 additions & 0 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"include": ["dist/**/*.js"],
"exclude": ["node_modules", "dist/**/*.d.ts"],
"compilerOptions": {
// Tells TypeScript to read JS files, as
// normally they are ignored as source files
"allowJs": true,
// Generate d.ts files
"declaration": true,
// This compiler run should
// only output d.ts files
"emitDeclarationOnly": true,
// Types should go into this directory.
// Removing this would place the .d.ts files
// next to the .js files
"outDir": "dist",
// go to js file when using IDE functions like
// "Go to Definition" in VSCode
"declarationMap": true
}
}
16 changes: 16 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"include": ["src/**/*.js"],
"exclude": [
"node_modules",
"dist",
"**/*.d.ts",
"**/*.stories.js",
"**/*.test.js",
"**/*.css"
],
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"noEmit": true
}
}