diff --git a/.gitignore b/.gitignore index d9d5ed5..3407d14 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ dist/ node_modules/ scratch/ +types/ diff --git a/package.json b/package.json index a0487d4..2571773 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,8 @@ "module-alias": "2", "rollup": "2", "rollup-plugin-terser": "7", - "snowpack": "3" + "snowpack": "3", + "typescript": "^4.8.3" }, "dependencies": { "htl": "0.3", diff --git a/src/range.js b/src/range.js index d129fe8..6e3d181 100644 --- a/src/range.js +++ b/src/range.js @@ -13,10 +13,43 @@ export function number(extent, options) { return createRange({extent}, options); } +/** + * @typedef {{ label: string, step: number, format: (d: number) => string, + * placeholder: string, transform: (d: number) => number, invert: (d: number) + * => number, validate: (d: number) => boolean, value: number, width: number, + * disabled: boolean }} Options + */ + +/** + * A Range input specifies a number between the given *extent* = [*min*, *max*] + * (inclusive). If an *extent* is not specified, it defaults to [0, 1]. The + * chosen number can be adjusted roughly with a slider, or precisely by typing a + * number. + * + * The available *options* are: + * + * * *label* - a label; either a string or an HTML element. + * * *step* - the step (precision); the interval between adjacent values. + * * *format* - a format function; defaults to [formatTrim](#formatTrim). + * * *placeholder* - a placeholder string for when the input is empty. + * * *transform* - an optional non-linear transform. + * * *invert* - the inverse transform. + * * *validate* - a function to check whether the number input is valid. + * * *value* - the initial value; defaults to (*min* + *max*) / 2. + * * *width* - the width of the input (not including the label). + * * *disabled* - whether input is disabled; defaults to false. + * + * @param {[number, number]} extent + * @param {Options} options + * @returns Element + */ export function range(extent = [0, 1], options) { return createRange({extent, range: true}, options); } +/** + * @returns Element + */ function createRange({ extent: [min, max], range diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..55ff635 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,19 @@ +{ + "compilerOptions": { + "target": "esnext", + "module": "esnext", + "lib": ["esnext", "dom"], + "strict": true, + "stripInternal": true, + "outDir": "dist", + "allowJs": true, + "declaration": true, + "declarationDir": "types", + "resolveJsonModule": true, + "moduleResolution": "node", + "paths": { + "@observablehq/inputs": ["./src/index.js"] + } + }, + "include": ["src/**/*"] +} diff --git a/yarn.lock b/yarn.lock index 81cedfa..e5ca6ae 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3857,6 +3857,11 @@ typedarray-to-buffer@^3.1.5: dependencies: is-typedarray "^1.0.0" +typescript@^4.8.3: + version "4.8.3" + resolved "https://registry.npmjs.org/typescript/-/typescript-4.8.3.tgz#d59344522c4bc464a65a730ac695007fdb66dd88" + integrity sha512-goMHfm00nWPa8UvR/CPSvykqf6dVV8x/dp0c5mFTMTIu0u0FlGWRioyy7Nn0PGAdHxpJZnuO/ut+PpQ8UiHAig== + unique-filename@^1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/unique-filename/-/unique-filename-1.1.1.tgz#1d69769369ada0583103a1e6ae87681b56573230"