Skip to content

Commit

Permalink
feat(core): add type declaration core
Browse files Browse the repository at this point in the history
See #366
  • Loading branch information
hperrin committed Apr 21, 2020
1 parent ce94fb7 commit be78dbf
Show file tree
Hide file tree
Showing 10 changed files with 622 additions and 50 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -458,9 +458,9 @@ PNotify options and default values.
* `textTrusted: false`<br>
Whether to trust the text or escape its contents. (Not allow HTML.)
* `styling: 'brighttheme'`<br>
What styling classes to use. (Can be 'brighttheme', 'material', or a styling object.) (Note that the Bootstrap modules provide a different default.)
What styling classes to use. (Can be 'brighttheme', 'material', another string provided by a module, or a styling object.)
* `icons: 'brighttheme'`<br>
What icons classes to use (Can be 'brighttheme', 'material', or an icon object.) (Note that the Font Awesome and Glyphicon modules provide a different default.)
What icons classes to use (Can be 'brighttheme', 'material', another string provided by a module, or an icon object.)
* `mode: 'no-preference'`<br>
Light or dark version of the theme, if supported by the styling. This overrides the CSS media query when a preference is given. (Can be 'no-preference', 'light', or 'dark'.)
* `addClass: ''`<br>
Expand Down
19 changes: 19 additions & 0 deletions libtests/typescript/index.ts
@@ -0,0 +1,19 @@
import {notice, Stack, defaultStack} from '@pnotify/core';

const myNotice = notice({
text: 'Hello.'
});

const stack = new Stack({
dir1: 'up'
});

stack.dir1;
defaultStack.dir1;

myNotice.text;
myNotice.text = document.createElement('span');
myNotice.text;

myNotice.closer;
myNotice.type;
22 changes: 22 additions & 0 deletions libtests/typescript/package-lock.json

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

15 changes: 15 additions & 0 deletions libtests/typescript/package.json
@@ -0,0 +1,15 @@
{
"name": "pnotify-typescript-test",
"version": "0.0.0",
"description": "Just a test.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "Apache-2.0",
"devDependencies": {
"@pnotify/core": "file:../../packages/core",
"typescript": "^3.8.3"
}
}
11 changes: 11 additions & 0 deletions libtests/typescript/tsconfig.json
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"sourceMap": true,
"outDir": "dist",
"strict": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true
}
}
1 change: 1 addition & 0 deletions packages/core/.npmignore
@@ -0,0 +1 @@
cleanup.js
193 changes: 193 additions & 0 deletions packages/core/Stack.d.ts
@@ -0,0 +1,193 @@
import Notice from './';

declare abstract class StackProperties {
/**
* The primary stacking direction. Can be `'up'`, `'down'`, `'right'`, or
* `'left'`.
*
* @default null
*/
dir1: 'up' | 'down' | 'right' | 'left' | null;
/**
* The secondary stacking direction. Should be a perpendicular direction to
* `dir1`. The notices will continue in this direction when they reach the
* edge of the viewport along `dir1`.
*
* @default null
*/
dir2: 'up' | 'down' | 'right' | 'left' | null;
/**
* Number of pixels from the edge of the context, relative to `dir1`, the
* first notice will appear. If null, the current position of the notice,
* whatever that is, will be used.
*
* @default null
*/
firstpos1: number | null;
/**
* Number of pixels from the edge of the context, relative to `dir2`, the
* first notice will appear. If null, the current position of the notice,
* whatever that is, will be used.
*
* @default null
*/
firstpos2: number | null;
/**
* Number of pixels between notices along `dir1`.
*
* @default 25
*/
spacing1: number;
/**
* Number of pixels between notices along `dir2`.
*
* @default 25
*/
spacing2: number;
/**
* Where, in the stack, to push new notices. Can be `'top'` or `'bottom'`.
*
* @default 'bottom'
*/
push: 'top' | 'bottom';
/**
* How many notices are allowed to be open in this stack at once.
*
* @default 1
*/
maxOpen: number;
/**
* The strategy to use to ensure `maxOpen`. Can be `'wait'`, which will cause
* new notices to wait their turn, or `'close'`, which will remove the oldest
* notice to make room for a new one.
*
* @default 'wait'
*/
maxStrategy: 'wait' | 'close';
/**
* Whether the notices that are closed to abide by `maxOpen` when
* `maxStrategy === 'close'` should wait and reopen in turn.
*
* @default true
*/
maxClosureCausesWait: boolean;
/**
* Whether the stack should be modal (`true`), modeless (`false`), or modalish
* (`'ish'`). Modalish stacks are cool.
* See https://sciactive.com/2020/02/11/the-modalish-notification-flow/.
*
* @default 'ish'
*/
modal: 'ish' | boolean;
/**
* Whether new notices that start waiting in a modalish stack should flash
* under the leader notice to show that they have been added.
*
* @default true
*/
modalishFlash: boolean;
/**
* Whether clicking on the modal overlay should close the stack's notices.
*
* @default true
*/
overlayClose: boolean;
/**
* Whether clicking on the modal to close notices also closes notices that
* have been pinned (`hide === false`).
*
* @default false
*/
overlayClosesPinned: boolean;
/**
* The DOM element this stack's notices should appear in.
*
* @default document.body
*/
context: HTMLElement;
}

export type StackOptions = Partial<StackProperties>;

/**
* A stack is an instance of the `Stack` class used to determine where to
* position notices and how they interact with each other.
*/
export default class Stack extends StackProperties {
/**
* An "array" of notices. It's actually built on the fly from the double
* linked list the notices are actually stored in.
*/
readonly notices: Notice[];
/**
* How many notices there are in the stack.
*/
readonly length: number;
/**
* When a stack is modalish, this is the notice that is open in the non-modal
* state.
*/
readonly leader: Notice | null;

constructor(options: StackOptions);

/**
* Run a callback for all the notices in the stack. `start` can be 'head',
* 'tail', 'oldest', or 'newest'. `dir` can be 'next', 'prev', 'older', or
* 'newer'.
* @param callback Function to run for each notice.
* @param options Controls which direction to iterate notices.
*/
forEach(
callback: (notice?: Notice) => void,
options?: {
/**
* Where to start the iteration.
*
* @default 'oldest'
*/
start: Notice | 'head' | 'tail' | 'oldest' | 'newest';
/**
* Which direction in the double linked list to iterate.
*
* @default 'newer'
*/
dir: 'next' | 'prev' | 'newer' | 'older';
/**
* Whether to skip notices whose open state is handled by a module.
*
* @default false
*/
skipModuleHandled: boolean;
}
): void;

/**
* Close all the notices in the stack.
* @param immediate Don't animate, just close immediately.
*/
close(immediate?: boolean): void;

/**
* Open all the notices in the stack.
* @param immediate Don't animate, just open immediately.
*/
open(immediate?: boolean): void;

/**
* Open the last closed/closing notice in the stack.
*/
openLast(): void;

/**
* Position all the notices in the stack.
*/
position(): void;

/**
* Queue a position call in that many milliseconds, unless another one is
* queued beforehand.
* @param milliseconds Time to wait before positioning. Default: 10.
*/
queuePosition(milliseconds?: number): void;
}

0 comments on commit be78dbf

Please sign in to comment.