Skip to content

Commit

Permalink
Merge pull request #27 from hirasso/feat/optimize-progress-calculation
Browse files Browse the repository at this point in the history
feat: optimize progress calculation, make logging optional
  • Loading branch information
hirasso authored Oct 18, 2024
2 parents ed36996 + db5a59b commit 4e786b7
Show file tree
Hide file tree
Showing 12 changed files with 1,000 additions and 273 deletions.
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ The type signature of the options object:
type Options = {
vertical: boolean;
horizontal: boolean;
debug: boolean;
}
```

Expand All @@ -104,6 +105,10 @@ Type: `boolean`, default: `true`. Should the vertical scroll position be mirrore

Type: `boolean`, default: `true`. Should the horizontal scroll position be mirrored?

### `debug`

Type: `boolean`, default: `true`. Should debug messages be printed to the console?

## API

To access ScrollMirror's API, you have to save a reference to the class during instaciation:
Expand All @@ -114,14 +119,15 @@ const mirror = new ScrollMirror(document.querySelectorAll(".scroller"));

### `mirror.progress`

Returns the current scroll progress in the form of `{x: number, y: number}`, where both x and y are a
Get the current scroll progress in the form of `{ x: number, y: number }`, where both x and y are a
number between 0-1

### `mirror.progress = value`

Sets the progress and scrolls all mirrored elements. For example:
Set the progress and scrolls all mirrored elements. For example:

```js
// for both directions
mirror.progress = { x: 0.2, y: 0.5 };
// or only set one direction
mirror.progress = { y: 0.5 };
Expand All @@ -131,7 +137,13 @@ mirror.progress = 0.5;

### `mirror.getScrollProgress(element: HTMLElement)`

Return the current progress of an element. The element doesn't _need_ to be one of the mirrored elements
Get the current progress of an element. The element doesn't _need_ to be one of the mirrored elements

```ts
const mirror = new ScrollMirror(document.querySelectorAll(".scroller"));
// ...sometime later:
console.log(mirror.getScrollProgress(document.querySelector(":root")));
```

## Motivation

Expand Down
20 changes: 12 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
"license": "ISC",
"type": "module",
"source": "./src/index.ts",
"main": "./dist/index.cjs",
"module": "./dist/index.module.js",
"unpkg": "./dist/index.umd.js",
"main": "./dist/ScrollMirror.cjs",
"module": "./dist/ScrollMirror.module.js",
"unpkg": "./dist/ScrollMirror.umd.js",
"types": "./dist/types/index.d.ts",
"exports": {
".": {
"types": "./dist/types/index.d.ts",
"import": "./dist/index.modern.js",
"require": "./dist/index.cjs"
"import": "./dist/ScrollMirror.modern.js",
"require": "./dist/ScrollMirror.cjs"
}
},
"files": [
Expand All @@ -38,12 +38,14 @@
"prepublishOnly": "pnpm run build",
"build": "pnpm run clean && pnpm run build:module && pnpm run build:bundle",
"build:module": "BROWSERSLIST_ENV=modern microbundle src/index.ts --format modern,esm,cjs",
"build:bundle": "BROWSERSLIST_ENV=production microbundle src/index.ts --format umd --external none",
"build:bundle": "BROWSERSLIST_ENV=production microbundle src/ScrollMirror.ts --format umd --external none",
"watch": "BROWSERSLIST_ENV=development microbundle src/index.ts --watch --format modern",
"docs:dev": "astro dev --root docs",
"docs:build": "astro build --root docs",
"docs:serve": "astro build --root docs && astro preview --root docs",
"test": "pnpm run test:e2e",
"test": "pnpm run test:unit && pnpm run test:e2e",
"test:unit": "vitest run --config ./tests/unit/vitest.config.ts",
"test:unit:watch": "vitest --config ./tests/unit/vitest.config.ts",
"test:e2e": "pnpm exec playwright test --config ./tests/e2e/config.playwright.ts",
"test:e2e:dev": "PLAYWRIGHT_ENV=dev pnpm run test:e2e --ui",
"test:e2e:install": "pnpm exec playwright install --with-deps"
Expand All @@ -54,8 +56,10 @@
"astro": "^4.15.9",
"astro-expressive-code": "^0.37.0",
"astro-feather": "^1.0.0",
"jsdom": "^25.0.1",
"microbundle": "^0.15.1",
"prettier": "^3.3.3",
"typescript": "^5.6.2"
"typescript": "^5.6.2",
"vitest": "^2.1.1"
}
}
Loading

0 comments on commit 4e786b7

Please sign in to comment.