-
Notifications
You must be signed in to change notification settings - Fork 38
add Angular adapter package #154
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
Open
adriandarian
wants to merge
4
commits into
spaansba:main
Choose a base branch
from
adriandarian:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e4d5f2b
refactor: simplify core data flow
adriandarian 15b01e6
fix(angular): address adapter review feedback
adriandarian 1706e6b
Add Angular docs and sidebar integration
adriandarian f12570d
fix(angular): address follow-up review feedback
adriandarian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| MIT License | ||
|
|
||
| Copyright (c) 2025 Bart Spaans | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| # @foresightjs/angular | ||
|
|
||
| [](https://www.npmjs.com/package/@foresightjs/angular) | ||
| [](https://www.npmjs.com/package/@foresightjs/angular) | ||
| [](https://opensource.org/licenses/MIT) | ||
| [](http://www.typescriptlang.org/) | ||
|
|
||
| Official Angular bindings for [ForesightJS](https://foresightjs.com/), a lightweight library that predicts user intent (mouse trajectory, keyboard navigation, scroll, touch) to trigger callbacks like prefetching _before_ the user interacts. | ||
|
|
||
| - **Docs:** [foresightjs.com/docs/angular/installation](https://foresightjs.com/docs/angular/installation) | ||
| - **Core library:** [`js.foresight`](https://www.npmjs.com/package/js.foresight) | ||
| - **Playground:** [foresightjs.com](https://foresightjs.com/#playground) | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| pnpm add @foresightjs/angular js.foresight | ||
| # or | ||
| npm install @foresightjs/angular js.foresight | ||
| ``` | ||
|
|
||
| Requires Angular 17+ | ||
|
|
||
| ## What's included | ||
|
|
||
| - `ForesightDirective` -> standalone `[fsForesight]` directive to register an element with a callback or full options object | ||
| - `ForesightService` -> injectable service for manual registration and manager event subscriptions | ||
| - `ForesightComponent` -> standalone wrapper component that registers its host element | ||
| - `injectForesightEvent` -> helper to subscribe to a ForesightManager event for the lifetime of the current injection context | ||
|
|
||
| ## Directive usage | ||
|
|
||
| ```ts | ||
| import { Component } from "@angular/core" | ||
| import { ForesightDirective } from "@foresightjs/angular" | ||
|
|
||
| @Component({ | ||
| standalone: true, | ||
| imports: [ForesightDirective], | ||
| template: ` | ||
| <a | ||
| href="/pricing" | ||
| [fsForesight]="prefetchPricing" | ||
| fsForesightName="pricing-link" | ||
| #foresight="foresight" | ||
| > | ||
| Pricing {{ foresight.state().isPredicted ? "(ready)" : "" }} | ||
| </a> | ||
| `, | ||
| }) | ||
| export class NavComponent { | ||
| readonly prefetchPricing = () => { | ||
| void fetch("/api/pricing") | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| For full options, bind an object: | ||
|
|
||
| ```html | ||
| <button | ||
| [fsForesight]="{ | ||
| callback: preloadCheckout, | ||
| name: 'checkout', | ||
| hitSlop: 40, | ||
| enabled: isCheckoutEnabled | ||
| }" | ||
| > | ||
| Checkout | ||
| </button> | ||
| ``` | ||
|
|
||
| ## Contributing | ||
|
|
||
| Please see the [contributing guidelines](https://github.com/spaansba/ForesightJS/blob/main/CONTRIBUTING.md). | ||
|
|
||
| ## License | ||
|
|
||
| [MIT](./LICENSE) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "$schema": "./node_modules/ng-packagr/ng-package.schema.json", | ||
| "dest": "dist", | ||
| "lib": { | ||
| "entryFile": "src/index.ts" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| { | ||
| "name": "@foresightjs/angular", | ||
| "version": "0.3.2", | ||
| "description": "Angular bindings for ForesightJS - directives and services to register elements with the ForesightManager from Angular components.", | ||
| "type": "module", | ||
| "sideEffects": false, | ||
| "scripts": { | ||
| "build": "ng-packagr -p ng-package.json -c tsconfig.lib.json", | ||
| "build:prod": "ng-packagr -p ng-package.json -c tsconfig.lib.prod.json && node ./scripts/remove-sourcemaps.mjs", | ||
| "dev": "ng-packagr -p ng-package.json -c tsconfig.lib.json --watch", | ||
| "lint": "eslint . --fix", | ||
| "test": "vitest", | ||
| "test:run": "vitest run", | ||
| "test:watch": "vitest --watch", | ||
| "prepublishOnly": "pnpm test:run && pnpm build:prod" | ||
| }, | ||
| "main": "./dist/fesm2022/foresightjs-angular.mjs", | ||
| "module": "./dist/fesm2022/foresightjs-angular.mjs", | ||
| "types": "./dist/types/foresightjs-angular.d.ts", | ||
| "exports": { | ||
| ".": { | ||
| "types": "./dist/types/foresightjs-angular.d.ts", | ||
| "default": "./dist/fesm2022/foresightjs-angular.mjs" | ||
| } | ||
| }, | ||
| "homepage": "https://foresightjs.com/", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/spaansba/ForesightJS", | ||
| "directory": "packages/foresightjs-angular" | ||
| }, | ||
| "publishConfig": { | ||
| "access": "public" | ||
| }, | ||
| "files": [ | ||
| "dist", | ||
| "README.md", | ||
| "LICENSE" | ||
| ], | ||
| "keywords": [ | ||
| "angular", | ||
| "foresight", | ||
| "js.foresight", | ||
| "foresightjs", | ||
| "angular-directive", | ||
| "prefetch", | ||
| "interaction-prediction", | ||
| "mouse-trajectory", | ||
| "ForesightDirective" | ||
| ], | ||
| "author": "Bart Spaans", | ||
| "license": "MIT", | ||
| "llms": "https://foresightjs.com/llms.txt", | ||
| "llmsFull": "https://foresightjs.com/llms-full.txt", | ||
| "peerDependencies": { | ||
| "@angular/core": "^17.0.0 || ^18.0.0 || ^19.0.0 || ^20.0.0 || ^21.0.0 || ^22.0.0", | ||
| "js.foresight": "^4.2.0" | ||
| }, | ||
| "dependencies": { | ||
| "tslib": "^2.8.1" | ||
| }, | ||
| "devDependencies": { | ||
| "@angular/compiler": "^22.0.2", | ||
| "@angular/compiler-cli": "^22.0.2", | ||
| "@angular/core": "^22.0.2", | ||
| "js.foresight": "workspace:*", | ||
| "ng-packagr": "^22.0.0", | ||
| "tslib": "^2.8.1", | ||
|
pullfrog[bot] marked this conversation as resolved.
Outdated
|
||
| "typescript": "^6.0.2", | ||
|
adriandarian marked this conversation as resolved.
|
||
| "vitest": "^4.1.2" | ||
| } | ||
| } | ||
16 changes: 16 additions & 0 deletions
16
packages/foresightjs-angular/scripts/remove-sourcemaps.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { readdirSync, rmSync } from "node:fs" | ||
| import { join } from "node:path" | ||
|
|
||
| const removeSourceMaps = directory => { | ||
| for (const entry of readdirSync(directory, { withFileTypes: true })) { | ||
| const path = join(directory, entry.name) | ||
|
|
||
| if (entry.isDirectory()) { | ||
| removeSourceMaps(path) | ||
| } else if (path.endsWith(".map")) { | ||
| rmSync(path) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| removeSourceMaps("dist") | ||
|
pullfrog[bot] marked this conversation as resolved.
|
||
69 changes: 69 additions & 0 deletions
69
packages/foresightjs-angular/src/components/ForesightComponent.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import { Component, ElementRef, Input, OnChanges, OnDestroy, computed, signal } from "@angular/core" | ||
| import { | ||
| createUnregisteredSnapshot, | ||
| type ForesightCallback, | ||
| type ForesightElementState, | ||
| type HitSlop, | ||
| } from "js.foresight" | ||
| import { ForesightService } from "../services/ForesightService" | ||
| import type { ForesightRegistration } from "../types" | ||
| import { resolveOptions } from "../utils/resolveOptions" | ||
|
|
||
| @Component({ | ||
| selector: "foresight", | ||
| standalone: true, | ||
| template: "<ng-content />", | ||
| }) | ||
| export class ForesightComponent implements OnChanges, OnDestroy { | ||
| @Input({ required: true }) callback!: ForesightCallback | ||
| @Input() foresightName?: string | ||
| @Input() hitSlop?: HitSlop | ||
| @Input() meta?: Record<string, unknown> | ||
| @Input() reactivateAfter?: number | ||
| @Input() enabled?: boolean | ||
|
|
||
| private readonly fallbackState = signal<Readonly<ForesightElementState>>( | ||
| createUnregisteredSnapshot(false) | ||
| ) | ||
| private readonly registration = signal<ForesightRegistration | null>(null) | ||
|
|
||
| readonly state = computed(() => this.registration()?.state() ?? this.fallbackState()) | ||
|
|
||
| constructor( | ||
| private readonly elementRef: ElementRef<HTMLElement>, | ||
| private readonly foresight: ForesightService | ||
| ) {} | ||
|
|
||
| ngOnChanges(): void { | ||
| const options = resolveOptions({ | ||
| value: this.callback, | ||
| name: this.foresightName, | ||
| hitSlop: this.hitSlop, | ||
| meta: this.meta, | ||
| reactivateAfter: this.reactivateAfter, | ||
| enabled: this.enabled, | ||
| }) | ||
|
|
||
| const registration = this.registration() | ||
| if (!options) { | ||
| registration?.unregister() | ||
| this.registration.set(null) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| if (registration) { | ||
| registration.update(options) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| const nextRegistration = this.foresight.register(this.elementRef.nativeElement, options) | ||
| this.registration.set(nextRegistration) | ||
| } | ||
|
|
||
| ngOnDestroy(): void { | ||
| this.registration()?.unregister() | ||
| this.registration.set(null) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.