├── .gitignore ├── LICENSE ├── README.md ├── index.d.ts └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Created by https://www.toptal.com/developers/gitignore/api/macos,windows,linux,visualstudiocode,jetbrains+all 3 | # Edit at https://www.toptal.com/developers/gitignore?templates=macos,windows,linux,visualstudiocode,jetbrains+all 4 | 5 | ### JetBrains+all ### 6 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider 7 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 8 | 9 | # User-specific stuff 10 | .idea/**/workspace.xml 11 | .idea/**/tasks.xml 12 | .idea/**/usage.statistics.xml 13 | .idea/**/dictionaries 14 | .idea/**/shelf 15 | 16 | # AWS User-specific 17 | .idea/**/aws.xml 18 | 19 | # Generated files 20 | .idea/**/contentModel.xml 21 | 22 | # Sensitive or high-churn files 23 | .idea/**/dataSources/ 24 | .idea/**/dataSources.ids 25 | .idea/**/dataSources.local.xml 26 | .idea/**/sqlDataSources.xml 27 | .idea/**/dynamic.xml 28 | .idea/**/uiDesigner.xml 29 | .idea/**/dbnavigator.xml 30 | 31 | # Gradle 32 | .idea/**/gradle.xml 33 | .idea/**/libraries 34 | 35 | # Gradle and Maven with auto-import 36 | # When using Gradle or Maven with auto-import, you should exclude module files, 37 | # since they will be recreated, and may cause churn. Uncomment if using 38 | # auto-import. 39 | # .idea/artifacts 40 | # .idea/compiler.xml 41 | # .idea/jarRepositories.xml 42 | # .idea/modules.xml 43 | # .idea/*.iml 44 | # .idea/modules 45 | # *.iml 46 | # *.ipr 47 | 48 | # CMake 49 | cmake-build-*/ 50 | 51 | # Mongo Explorer plugin 52 | .idea/**/mongoSettings.xml 53 | 54 | # File-based project format 55 | *.iws 56 | 57 | # IntelliJ 58 | out/ 59 | 60 | # mpeltonen/sbt-idea plugin 61 | .idea_modules/ 62 | 63 | # JIRA plugin 64 | atlassian-ide-plugin.xml 65 | 66 | # Cursive Clojure plugin 67 | .idea/replstate.xml 68 | 69 | # Crashlytics plugin (for Android Studio and IntelliJ) 70 | com_crashlytics_export_strings.xml 71 | crashlytics.properties 72 | crashlytics-build.properties 73 | fabric.properties 74 | 75 | # Editor-based Rest Client 76 | .idea/httpRequests 77 | 78 | # Android studio 3.1+ serialized cache file 79 | .idea/caches/build_file_checksums.ser 80 | 81 | ### JetBrains+all Patch ### 82 | # Ignores the whole .idea folder and all .iml files 83 | # See https://github.com/joeblau/gitignore.io/issues/186 and https://github.com/joeblau/gitignore.io/issues/360 84 | 85 | .idea/ 86 | 87 | # Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-249601023 88 | 89 | *.iml 90 | modules.xml 91 | .idea/misc.xml 92 | *.ipr 93 | 94 | # Sonarlint plugin 95 | .idea/sonarlint 96 | 97 | ### Linux ### 98 | *~ 99 | 100 | # temporary files which can be created if a process still has a handle open of a deleted file 101 | .fuse_hidden* 102 | 103 | # KDE directory preferences 104 | .directory 105 | 106 | # Linux trash folder which might appear on any partition or disk 107 | .Trash-* 108 | 109 | # .nfs files are created when an open file is removed but is still being accessed 110 | .nfs* 111 | 112 | ### macOS ### 113 | # General 114 | .DS_Store 115 | .AppleDouble 116 | .LSOverride 117 | 118 | # Icon must end with two \r 119 | Icon 120 | 121 | 122 | # Thumbnails 123 | ._* 124 | 125 | # Files that might appear in the root of a volume 126 | .DocumentRevisions-V100 127 | .fseventsd 128 | .Spotlight-V100 129 | .TemporaryItems 130 | .Trashes 131 | .VolumeIcon.icns 132 | .com.apple.timemachine.donotpresent 133 | 134 | # Directories potentially created on remote AFP share 135 | .AppleDB 136 | .AppleDesktop 137 | Network Trash Folder 138 | Temporary Items 139 | .apdisk 140 | 141 | ### VisualStudioCode ### 142 | .vscode/* 143 | !.vscode/settings.json 144 | !.vscode/tasks.json 145 | !.vscode/launch.json 146 | !.vscode/extensions.json 147 | *.code-workspace 148 | 149 | # Local History for Visual Studio Code 150 | .history/ 151 | 152 | ### VisualStudioCode Patch ### 153 | # Ignore all local history of files 154 | .history 155 | .ionide 156 | 157 | ### Windows ### 158 | # Windows thumbnail cache files 159 | Thumbs.db 160 | Thumbs.db:encryptable 161 | ehthumbs.db 162 | ehthumbs_vista.db 163 | 164 | # Dump file 165 | *.stackdump 166 | 167 | # Folder config file 168 | [Dd]esktop.ini 169 | 170 | # Recycle Bin used on file shares 171 | $RECYCLE.BIN/ 172 | 173 | # Windows Installer files 174 | *.cab 175 | *.msi 176 | *.msix 177 | *.msm 178 | *.msp 179 | 180 | # Windows shortcuts 181 | *.lnk 182 | 183 | # End of https://www.toptal.com/developers/gitignore/api/macos,windows,linux,visualstudiocode,jetbrains+all 184 | .fleet/ 185 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Luke Warlow 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Navigation API Types 2 | 3 | [![npm](https://img.shields.io/npm/v/navigation-api-types.svg?style=flat-square)](https://www.npmjs.com/package/navigation-api-types) 4 | 5 | Type definition for [`Navigation API`](https://developer.mozilla.org/en-US/docs/Web/API/Navigation_API) 6 | 7 | ### Install 8 | 9 | ```shell 10 | $ npm i -D navigation-api-types 11 | ``` 12 | ### Usage 13 | 14 | #### Make types visible in specific files 15 | 16 | Add a [TypesScript triple-slash directive](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html#-reference-types-) 17 | as follows in any code-containing '.ts' file you want these types to be available in: 18 | 19 | ```typescript 20 | // Add types for window.navigation for use in this file. See https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html#-reference-types- for more info. 21 | /// 22 | 23 | console.log(window.navigation) // no type error! 24 | ``` 25 | 26 | #### Make types visible globally in all source files within a project 27 | 28 | Create a `.d.ts` file anywhere in your project so that it is visible to TypeScript according to your `tsconfig.json` settings. For 29 | example, it could be at `src/global.d.ts` or `src/navigation-api-types.d.ts`. 30 | 31 | Add a [TypesScript triple-slash directive](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html#-reference-types-) as follows: 32 | 33 | ```typescript 34 | // Add types for window.navigation ambiently for implicit use in the entire project. See https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html#-reference-types- for more info. 35 | /// 36 | ``` 37 | 38 | This exposes the types *ambiently* so they are available without any `import` or `require` statements. TypeScript will simply know about them everywhere. 39 | 40 | **Important**: do not add any `import` or `export` statements to this file, or it will stop working ambiently. Doing that 41 | changes it in TypeScript's view from a "script" to a "module", and the rules about ambient types change in that case. 42 | 43 | ## License 44 | 45 | This project is licensed under the [MIT License](https://github.com/lukewarlow/navigation-api-types/blob/master/LICENSE). 46 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | 2 | /** @see https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigation */ 3 | interface Navigation extends EventTarget { 4 | entries(): NavigationHistoryEntry[]; 5 | readonly currentEntry?: NavigationHistoryEntry; 6 | updateCurrentEntry(options: NavigationUpdateCurrentEntryOptions): void; 7 | readonly transition?: NavigationTransition; 8 | 9 | readonly canGoBack: boolean; 10 | readonly canGoForward: boolean; 11 | 12 | navigate(url: string, options?: NavigationNavigateOptions): NavigationResult; 13 | reload(options?: NavigationReloadOptions): NavigationResult; 14 | 15 | traverseTo(key: string, options?: NavigationOptions): NavigationResult; 16 | back(options?: NavigationOptions): NavigationResult; 17 | forward(options?: NavigationOptions): NavigationResult; 18 | 19 | onnavigate: ((this: Navigation, ev: NavigationEventMap["navigate"]) => any) | null; 20 | onnavigatesuccess: ((this: Navigation, ev: NavigationEventMap["navigatesuccess"]) => any) | null; 21 | onnavigateerror: ((this: Navigation, ev: NavigationEventMap["navigateerror"]) => any) | null; 22 | oncurrententrychange: ((this: Navigation, ev: NavigationEventMap["currententrychange"]) => any) | null; 23 | 24 | addEventListener(type: K, listener: (this: Navigation, ev: NavigationEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void; 25 | addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void; 26 | removeEventListener(type: K, listener: (this: Navigation, ev: NavigationEventMap[K]) => any, options?: boolean | EventListenerOptions): void; 27 | removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void; 28 | } 29 | 30 | interface NavigationEventMap { 31 | navigate: NavigateEvent; 32 | navigatesuccess: Event; 33 | navigateerror: ErrorEvent; 34 | currententrychange: NavigationCurrentEntryChangeEvent; 35 | } 36 | 37 | declare const navigation: Navigation | undefined; 38 | 39 | declare interface Window extends WindowNavigation {} 40 | 41 | declare interface WindowNavigation { 42 | readonly navigation?: Navigation; 43 | } 44 | 45 | /** @see https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigateevent */ 46 | interface NavigateEvent extends Event { 47 | readonly navigationType: NavigationApiNavigationType; 48 | readonly destination: NavigationDestination; 49 | readonly canIntercept: boolean; 50 | readonly userInitiated: boolean; 51 | readonly hashChange: boolean; 52 | readonly signal: AbortSignal; 53 | readonly formData: FormData | null; 54 | readonly downloadRequest: string | null; 55 | readonly info: any; 56 | readonly hasUAVisualTransition: boolean; 57 | readonly sourceElement: Element | null; 58 | 59 | intercept(options?: NavigationInterceptOptions): void; 60 | /** 61 | * Not in HTML spec but implemented in Chromium 62 | * @see https://github.com/WICG/navigation-api?tab=readme-ov-file#deferred-commit 63 | * @experimental 64 | */ 65 | commit(): void; 66 | /** 67 | * Not in HTML spec but implemented in Chromium 68 | * @see https://github.com/WICG/navigation-api?tab=readme-ov-file#redirects-during-deferred-commit 69 | * @experimental 70 | */ 71 | redirect(): void; 72 | scroll(): void; 73 | } 74 | 75 | declare var NavigateEvent: { 76 | prototype: NavigateEvent; 77 | new(type: string, eventInit: NavigateEventInit): Event; 78 | }; 79 | 80 | /** @see https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigateeventinit */ 81 | interface NavigateEventInit extends EventInit { 82 | navigationType?: NavigationApiNavigationType; 83 | destination: NavigationDestination; 84 | canIntercept?: boolean; 85 | userInitiated?: boolean; 86 | hashChange?: boolean; 87 | signal: AbortSignal; 88 | formData?: FormData | null; 89 | downloadRequest?: string | null; 90 | info?: any; 91 | hasUAVisualTransition?: boolean; 92 | sourceElement?: Element | null; 93 | } 94 | 95 | /** @see https://html.spec.whatwg.org/multipage/nav-history-apis.html#the-navigationcurrententrychangeevent-interface */ 96 | interface NavigationCurrentEntryChangeEvent extends Event { 97 | readonly navigationType?: NavigationApiNavigationType; 98 | readonly from: NavigationHistoryEntry; 99 | } 100 | 101 | declare var NavigationCurrentEntryChangeEvent: { 102 | prototype: NavigationCurrentEntryChangeEvent; 103 | new(type: string, eventInit: NavigationCurrentEntryChangeEventInit): Event; 104 | }; 105 | 106 | /** @see https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigationcurrententrychangeeventinit */ 107 | interface NavigationCurrentEntryChangeEventInit extends EventInit { 108 | navigationType?: NavigationApiNavigationType; 109 | destination: NavigationHistoryEntry; 110 | } 111 | 112 | /** @see https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigationhistoryentry */ 113 | interface NavigationHistoryEntry extends EventTarget { 114 | readonly url: string | null; 115 | readonly key: string; 116 | readonly id: string; 117 | readonly index: number; 118 | readonly sameDocument: boolean; 119 | 120 | getState(): any; 121 | 122 | ondispose: ((this: NavigationHistoryEntry, ev: Event) => any) | null; 123 | 124 | addEventListener(type: "dispose", callback: (this: NavigationHistoryEntry, ev: Event) => any, options?: boolean | AddEventListenerOptions): void; 125 | addEventListener(type: string, callback: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void; 126 | removeEventListener(type: "dispose", callback: (this: NavigationHistoryEntry, ev: Event) => any, options?: boolean | EventListenerOptions): void; 127 | removeEventListener(type: string, callback: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void; 128 | } 129 | 130 | /** @see https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigationdestination */ 131 | interface NavigationDestination { 132 | readonly url: string; 133 | readonly key: string | null; 134 | readonly id: string | null; 135 | readonly index: number; 136 | readonly sameDocument: boolean; 137 | 138 | getState(): any; 139 | } 140 | 141 | /** @see https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigationupdatecurrententryoptions */ 142 | interface NavigationUpdateCurrentEntryOptions { 143 | state: any; 144 | } 145 | 146 | /** @see https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigationoptions */ 147 | interface NavigationOptions { 148 | info?: any; 149 | } 150 | 151 | /** @see https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigationnavigateoptions */ 152 | interface NavigationNavigateOptions extends NavigationOptions { 153 | state?: any; 154 | // Defaults to "auto" 155 | history?: NavigationHistoryBehavior; 156 | } 157 | 158 | /** @see https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigationreloadoptions */ 159 | interface NavigationReloadOptions extends NavigationOptions { 160 | state?: any; 161 | } 162 | 163 | /** @see https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigationtransition */ 164 | interface NavigationTransition { 165 | readonly navigationType: NavigationApiNavigationType; 166 | readonly from: NavigationHistoryEntry; 167 | readonly finished: Promise; 168 | } 169 | 170 | /** @see https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigationresult */ 171 | interface NavigationResult { 172 | committed: Promise; 173 | finished: Promise; 174 | } 175 | 176 | /** @see https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigationinterceptoptions */ 177 | interface NavigationInterceptOptions { 178 | handler?: NavigationInterceptHandler; 179 | focusReset?: NavigationFocusReset; 180 | scroll?: NavigationScrollBehavior; 181 | /** 182 | * Not in HTML spec but implemented in Chromium 183 | * @see https://github.com/WICG/navigation-api?tab=readme-ov-file#deferred-commit 184 | * @experimental 185 | */ 186 | commit?: NavigationCommitBehavior; 187 | } 188 | 189 | /** @see https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigationtype */ 190 | type NavigationApiNavigationType = "reload" | "push" | "replace" | "traverse"; 191 | 192 | /** @see https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigationhistorybehavior */ 193 | type NavigationHistoryBehavior = "auto" | "push" | "replace"; 194 | 195 | /** @see https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigationintercepthandler */ 196 | type NavigationInterceptHandler = () => Promise; 197 | 198 | /** @see https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigationfocusreset */ 199 | type NavigationFocusReset = "after-transition" | "manual"; 200 | 201 | /** @see https://html.spec.whatwg.org/multipage/nav-history-apis.html#navigationscrollbehavior */ 202 | type NavigationScrollBehavior = "after-transition" | "manual"; 203 | 204 | /** 205 | * Not in HTML spec but implemented in Chromium 206 | * @see https://github.com/WICG/navigation-api?tab=readme-ov-file#deferred-commit 207 | * @experimental 208 | */ 209 | type NavigationCommitBehavior = "after-transition" | "immediate"; 210 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "navigation-api-types", 3 | "version": "0.6.1", 4 | "description": "Type definition for the Navigation API", 5 | "keywords": [ 6 | "navigation-api", 7 | "Navigation API", 8 | "app-history-api", 9 | "App History API", 10 | "TypeScript", 11 | "types" 12 | ], 13 | "repository": "https://github.com/lukewarlow/navigation-api-types.git", 14 | "types": "index.d.ts", 15 | "author": "Luke Warlow", 16 | "license": "MIT" 17 | } 18 | --------------------------------------------------------------------------------