├── .DS_Store
├── .gitignore
├── .vscode
└── settings.json
├── LICENSE
├── README.md
├── jest.config.js
├── package.json
├── public
├── .DS_Store
└── index.html
├── src
├── demo.ts
├── index.ts
└── main.test.ts
├── tsconfig.json
├── webpack.config.js
└── yarn.lock
/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arturbien/browser-font-size-observer/2ced05b05f96eedb15c265230b7c25a459af7ef9/.DS_Store
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 |
2 | # dependencies
3 | /node_modules
4 | /dist
--------------------------------------------------------------------------------
/.vscode/settings.json:
--------------------------------------------------------------------------------
1 | {
2 | "typescript.tsdk": "node_modules/typescript/lib"
3 | }
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2023 Artur Bień
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 | # browser-font-size-observer
2 |
3 |
4 |
5 |
6 |
7 |
8 | A library that tracks the browser's font size setting.
9 |
10 | ## Installation
11 |
12 | To install, run:
13 |
14 | ```sh
15 | # yarn
16 | yarn add browser-font-size-observer
17 |
18 | # npm
19 | npm install browser-font-size-observer
20 | ```
21 |
22 | ## Usage
23 |
24 | ```js
25 | import BrowserFontSizeObserver from "browser-font-size-observer";
26 |
27 | const observer = new BrowserFontSizeObserver((state) => {
28 | console.log(state);
29 | });
30 |
31 | console.log(observer.state); // { browserFontSize: 16, ratio: 1 }
32 |
33 | // To stop observing
34 | observer.disconnect();
35 | ```
36 |
37 | ## Use Case
38 |
39 | One potential use case for this library is in browser extensions, chatbots or other widgets that are embedded on top of another website. In these scenarios, the containing website may have a fixed font size, which means that any changes to the browser's font size setting may not be reflected in the extension or a widget.
40 |
41 | By using the BrowserFontSizeObserver, the extension or widget can track changes to the browser's font size setting and adjust its own layout or styles accordingly, even if the containing website does not change.
42 |
43 | ## Browser Compatibility
44 |
45 | It is worth noting that different browsers may behave differently when it comes to changes in the browser's font size setting.
46 |
47 | In Chrome and Firefox, changes to the browser's font size setting will only affect webpages whose html tag has a font-size specified in the rem or em unit. If the html tag has a font-size specified in any other unit (such as px), the font size of the webpage will not change when the browser's font size setting is changed.
48 |
49 | Safari, on the other hand, will change the font size of a webpage regardless of the unit used for the font-size of the html tag.
50 |
51 | Keep this in mind when using the BrowserFontSizeObserver library, as the resulting ratio of the state object may not always match the browser's font size setting in these different browsers.
52 |
53 | ## API
54 |
55 | ### `BrowserFontSizeObserver`
56 |
57 | #### `constructor(onChangeHandler?: OnChangeHandler)`
58 |
59 | Creates an instance of `BrowserFontSizeObserver`.
60 |
61 | ##### Parameters
62 |
63 | - `onChangeHandler` (optional): A callback function that will be called whenever the browser font size changes. The callback will be passed an object with the following shape: `{ browserFontSize: number, ratio: number }`.
64 |
65 | #### `disconnect()`
66 |
67 | Stops observing for changes to the browser font size.
68 |
69 | #### `state: State`
70 |
71 | An object with the following shape: `{ browserFontSize: number, ratio: number }`.
72 |
73 | ## License
74 |
75 | This library is licensed under the MIT license. See [LICENSE](./LICENSE) for more details.
76 |
--------------------------------------------------------------------------------
/jest.config.js:
--------------------------------------------------------------------------------
1 | /** @type {import('ts-jest').JestConfigWithTsJest} */
2 | module.exports = {
3 | preset: 'ts-jest',
4 | testEnvironment: 'jsdom',
5 | testEnvironmentOptions: { "resources": "usable" }
6 | };
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "browser-font-size-observer",
3 | "version": "0.1.1",
4 | "author": "Artur Bień (https://www.linkedin.com/in/arturbien/)",
5 | "description": "Detect and observe changes to the browser font size setting",
6 | "keywords": [
7 | "detect",
8 | "browser",
9 | "font size",
10 | "font"
11 | ],
12 | "files": ["./dist"],
13 | "repository": "git@github.com:arturbien/browser-font-size-observer.git",
14 | "main": "./dist/index.js",
15 | "types": "./dist/index.d.ts",
16 | "funding": [
17 | {
18 | "type": "paypal",
19 | "url": "https://www.paypal.me/react95"
20 | },
21 | {
22 | "type": "patreon",
23 | "url": "https://www.patreon.com/arturbien"
24 | }
25 | ],
26 | "publishConfig": {
27 | "access": "public"
28 | },
29 | "scripts": {
30 | "start": "webpack-dev-server --open",
31 | "test": "jest",
32 | "build": "webpack",
33 | "prepare": "webpack"
34 | },
35 | "license": "MIT",
36 | "devDependencies": {
37 | "@types/jest": "^29.2.5",
38 | "@types/jsdom": "^20.0.1",
39 | "jest": "^29.3.1",
40 | "jest-environment-jsdom": "^29.3.1",
41 | "jsdom": "^20.0.3",
42 | "ts-jest": "^29.0.3",
43 | "ts-loader": "^9.3.1",
44 | "typescript": "^4.9.4",
45 | "webpack": "^5.75.0",
46 | "webpack-cli": "^3.3.6",
47 | "webpack-dev-server": "^3.7.2"
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/public/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arturbien/browser-font-size-observer/2ced05b05f96eedb15c265230b7c25a459af7ef9/public/.DS_Store
--------------------------------------------------------------------------------
/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | Browser font size observer
8 |
35 |
36 |
37 | Test
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/src/demo.ts:
--------------------------------------------------------------------------------
1 | import BrowserFontSizeObserver from "browser-font-size-observer";
2 |
3 | const h1 = document.createElement("h1");
4 | h1.innerHTML = "Nothing yet.";
5 |
6 | document.body.appendChild(h1);
7 |
8 | const browserFontSizeObserver = new BrowserFontSizeObserver((state) => {
9 | h1.innerHTML = `${state.browserFontSize} ${state.ratio}`;
10 | });
11 |
--------------------------------------------------------------------------------
/src/index.ts:
--------------------------------------------------------------------------------
1 | type State = {
2 | browserFontSize: number;
3 | ratio: number;
4 | };
5 | type OnChangeHandler = (state: State) => void;
6 |
7 | class BrowserFontSizeObserver {
8 | static #customElementName = "browser-font-size";
9 | static #pixel: HTMLElement | null = null;
10 |
11 | static #createPixel() {
12 | if (!BrowserFontSizeObserver.#pixel) {
13 | const customElement = document.createElement(
14 | BrowserFontSizeObserver.#customElementName
15 | );
16 | Object.assign(customElement.style, {
17 | position: "fixed",
18 | left: "0",
19 | top: "0",
20 | width: "0px",
21 | height: "0px",
22 | overflow: "hidden",
23 | pointerEvents: "none",
24 | clip: "rect(0 0 0 0)",
25 | clipPath: "inset(50%)",
26 | });
27 |
28 | const shadowRoot = customElement.attachShadow({ mode: "closed" });
29 | const iframe = document.createElement("iframe");
30 | shadowRoot.appendChild(iframe);
31 |
32 | document.documentElement.appendChild(customElement);
33 | iframe.contentWindow.document.write(
34 | ``
35 | );
36 | const pixel = iframe.contentWindow.document.querySelector(
37 | "div"
38 | ) as HTMLDivElement;
39 |
40 | BrowserFontSizeObserver.#pixel = pixel;
41 | }
42 | return BrowserFontSizeObserver.#pixel;
43 | }
44 |
45 | #state: State | null = null;
46 | #resizeObserver: ResizeObserver | null = null;
47 | #onChangeHandler: OnChangeHandler | null = null;
48 |
49 | #setState(size: number) {
50 | this.#state = {
51 | browserFontSize: size,
52 | ratio: size / 16,
53 | };
54 | }
55 |
56 | constructor(onChangeHandler?: OnChangeHandler) {
57 | const pixel = BrowserFontSizeObserver.#createPixel();
58 | const size = pixel.offsetWidth;
59 | this.#setState(size);
60 |
61 | if (onChangeHandler) {
62 | this.#onChangeHandler = onChangeHandler;
63 | }
64 | this.#resizeObserver = new ResizeObserver((entries) => {
65 | for (const entry of entries) {
66 | const size = entry.borderBoxSize
67 | ? entry.borderBoxSize[0].inlineSize
68 | : (entry.target as HTMLElement).offsetWidth;
69 |
70 | this.#setState(size);
71 | this.#onChangeHandler?.(this.#state);
72 | }
73 | });
74 | this.#resizeObserver.observe(pixel);
75 | }
76 |
77 | disconnect() {
78 | this.#onChangeHandler = null;
79 | }
80 |
81 | get state() {
82 | return this.#state;
83 | }
84 | }
85 |
86 | export default BrowserFontSizeObserver;
87 |
--------------------------------------------------------------------------------
/src/main.test.ts:
--------------------------------------------------------------------------------
1 | import { JSDOM } from "jsdom";
2 | import BrowserFontSizeObserver from "./";
3 |
4 | test("A pixel is created", () => {
5 | const browserFontSizeObserver = new BrowserFontSizeObserver();
6 | const customElement =
7 | document.documentElement.querySelector("browser-font-size");
8 | expect(customElement).toBeTruthy();
9 | });
10 |
--------------------------------------------------------------------------------
/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "outDir": "./dist/",
4 | "module": "commonjs",
5 | "moduleResolution": "node",
6 | "target": "es6",
7 | "lib": [
8 | "es2017",
9 | "DOM"
10 | ],
11 | "declaration": true,
12 | "noErrorTruncation": true,
13 | "noFallthroughCasesInSwitch": true,
14 | "noImplicitAny": true,
15 | "noImplicitReturns": true,
16 | "paths": {
17 | "browser-font-size-observer": [
18 | "./src/index"
19 | ]
20 | }
21 | }
22 | }
--------------------------------------------------------------------------------
/webpack.config.js:
--------------------------------------------------------------------------------
1 | const path = require("path");
2 | // https://webpack.js.org/guides/author-libraries/
3 | module.exports = {
4 | entry: {
5 | index: "./src/index.ts",
6 | demo: "./src/demo.ts"
7 | },
8 | devtool: "inline-source-map",
9 | devServer: {
10 | contentBase: "./public",
11 | watchContentBase: true,
12 | port: 8080,
13 | },
14 | module: {
15 | rules: [
16 | {
17 | test: /\.tsx?$/,
18 | use: "ts-loader",
19 | exclude: /node_modules/
20 | }
21 | ]
22 | },
23 | resolve: {
24 | extensions: [".tsx", ".ts", ".js"],
25 | alias: {
26 | "browser-font-size-observer": path.resolve(__dirname, "src/index")
27 | }
28 | },
29 | output: {
30 | filename: '[name].js',
31 | path: path.resolve(__dirname, "dist"),
32 | libraryExport: "default",
33 | libraryTarget: "umd"
34 | }
35 | };
36 |
--------------------------------------------------------------------------------