Skip to content

Commit 16e15ba

Browse files
committed
initial commit
0 parents  commit 16e15ba

File tree

7 files changed

+168
-0
lines changed

7 files changed

+168
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
DS_Store

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Svelte Fit Text
2+
3+
An extremely simple, no dependency fit text library.

index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export declare const fit: (node: HTMLElement, min_size?: number, max_size?: number) => {
2+
destroy: () => void;
3+
};
4+
export declare const parent_style = "display: inline-block;\n\t\twidth: 100%;\n\t\theight: 100%;\n\t\toverflow-x: auto;\n\t\toverflow-y: hidden;";

index.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
export const fit = (node, min_size = 12, max_size = 100) => {
2+
const on_resize = () => {
3+
if (node.parentElement) {
4+
resize_text(node, node.parentElement, min_size, max_size);
5+
}
6+
};
7+
const resize_observer = new ResizeObserver(on_resize);
8+
resize_observer.observe(node === null || node === void 0 ? void 0 : node.parentElement);
9+
return {
10+
destroy() {
11+
resize_observer.disconnect();
12+
},
13+
};
14+
};
15+
function is_overflow({ clientWidth, clientHeight, scrollWidth, scrollHeight, }) {
16+
return scrollWidth > clientWidth || scrollHeight > clientHeight;
17+
}
18+
function resize_text(element, parent, min_size, max_size) {
19+
let i = min_size;
20+
let overflow = false;
21+
let size = min_size;
22+
element.style.fontSize = `${size}px`;
23+
while (!overflow && i < max_size) {
24+
overflow = is_overflow(parent);
25+
if (!overflow) {
26+
// If not overflowing, increase the font size
27+
element.style.fontSize = `${i}px`;
28+
i++;
29+
}
30+
}
31+
size = i - 2;
32+
// console.log('overflow', i, size, element)
33+
element.style.fontSize = `${size}px`;
34+
}
35+
export const parent_style = `display: inline-block;
36+
width: 100%;
37+
height: 100%;
38+
overflow-x: auto;
39+
overflow-y: hidden;`;

index.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
export const fit = (
2+
node: HTMLElement,
3+
min_size: number = 12,
4+
max_size: number = 100
5+
): { destroy: () => void } => {
6+
const on_resize = (): void => {
7+
if (node.parentElement) {
8+
resize_text(node, node.parentElement, min_size, max_size)
9+
}
10+
}
11+
12+
const resize_observer = new ResizeObserver(on_resize)
13+
resize_observer.observe(node?.parentElement as HTMLElement)
14+
15+
return {
16+
destroy(): void {
17+
resize_observer.disconnect()
18+
},
19+
}
20+
}
21+
22+
function is_overflow({
23+
clientWidth,
24+
clientHeight,
25+
scrollWidth,
26+
scrollHeight,
27+
}: HTMLElement): boolean {
28+
return scrollWidth > clientWidth || scrollHeight > clientHeight
29+
}
30+
31+
function resize_text(
32+
element: HTMLElement,
33+
parent: HTMLElement,
34+
min_size: number,
35+
max_size: number
36+
): void {
37+
let i: number = min_size
38+
let overflow: boolean = false
39+
let size: number = min_size
40+
element.style.fontSize = `${size}px`
41+
42+
while (!overflow && i < max_size) {
43+
overflow = is_overflow(parent)
44+
45+
if (!overflow) {
46+
// If not overflowing, increase the font size
47+
element.style.fontSize = `${i}px`
48+
i++
49+
}
50+
}
51+
size = i - 2
52+
// console.log('overflow', i, size, element)
53+
element.style.fontSize = `${size}px`
54+
}
55+
56+
export const parent_style = `display: inline-block;
57+
width: 100%;
58+
height: 100%;
59+
overflow-x: auto;
60+
overflow-y: hidden;`

package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"author": "Scott Tolinski",
3+
"description": "",
4+
"devDependencies": {
5+
"typescript": "^4.8.2"
6+
},
7+
"exports": {
8+
"default": "./index.js"
9+
},
10+
"keywords": [],
11+
"license": "ISC",
12+
"main": "index.js",
13+
"name": "@leveluptuts/svelte-fit",
14+
"scripts": {
15+
"build": "tsc",
16+
"test": "echo \"Error: no test specified\" && exit 1"
17+
},
18+
"type": "module",
19+
"version": "1.0.0"
20+
}

tsconfig.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"compilerOptions": {
3+
/* Visit https://aka.ms/tsconfig.json to read more about this file */
4+
5+
/* Basic Options */
6+
// "incremental": true, /* Enable incremental compilation */
7+
"target": "ES2015",
8+
"module": "ES2020",
9+
10+
"declaration": true /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
11+
12+
/* Strict Type-Checking Options */
13+
"strict": true /* Enable all strict type-checking options. */,
14+
/* Module Resolution Options */
15+
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
16+
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
17+
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
18+
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
19+
// "typeRoots": [], /* List of folders to include type definitions from. */
20+
// "types": [], /* Type declaration files to be included in compilation. */
21+
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
22+
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
23+
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
24+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
25+
26+
/* Source Map Options */
27+
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
28+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
29+
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
30+
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
31+
32+
/* Experimental Options */
33+
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
34+
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
35+
36+
/* Advanced Options */
37+
"skipLibCheck": true /* Skip type checking of declaration files. */,
38+
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
39+
}
40+
}

0 commit comments

Comments
 (0)