Skip to content

Commit 866ab44

Browse files
committed
chore: Add publish config
1 parent a6ac33c commit 866ab44

File tree

5 files changed

+83
-27
lines changed

5 files changed

+83
-27
lines changed

packages/react/package.json

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,40 @@
33
"version": "0.1.0",
44
"description": "React hooks and components for building responsive websites.",
55
"license": "MIT",
6+
"homepage": "https://github.com/Milesight-IoT/responsive-toolkit",
7+
"repository": "https://github.com/Milesight-IoT/responsive-toolkit.git",
8+
"bugs": {
9+
"url": "https://github.com/Milesight-IoT/responsive-toolkit/issues"
10+
},
11+
"keywords": [
12+
"responsive",
13+
"react",
14+
"hook",
15+
"useMediaQuery",
16+
"breakpoints"
17+
],
18+
"exports": {
19+
".": {
20+
"import": "./dist/index.mjs",
21+
"require": "./dist/index.js"
22+
}
23+
},
24+
"types": "dist/index.d.ts",
625
"files": [
726
"dist"
827
],
928
"publishConfig": {
1029
"registry": "https://registry.npmjs.org/"
1130
},
1231
"scripts": {
13-
"dev": "echo \"No dev script provided for this package.\"",
14-
"build": "echo \"No build script provided for this package.\"",
32+
"dev": "tsup --watch",
33+
"build": "tsup",
1534
"test": "vitest run",
1635
"test:watch": "vitest",
1736
"coverage": "vitest run --coverage",
1837
"lint": "eslint ./src/*",
19-
"lint:fix": "eslint --fix ./src/*"
38+
"lint:fix": "eslint --fix ./src/*",
39+
"prepublish": "pnpm run build"
2040
},
2141
"peerDependencies": {
2242
"react": "^17 || ^18 || ^19"

packages/react/src/types/index.d.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@
22
* Remove properties `K` from `T`.
33
* Distributive for union types.
44
*/
5-
declare type DistributiveOmit<T, K extends keyof any> = T extends any ? Omit<T, K> : never;
5+
export type DistributiveOmit<T, K extends keyof any> = T extends any ? Omit<T, K> : never;
66

77
/**
88
* Like `T & U`, but using the value types from `U` where their properties overlap.
99
*/
10-
declare type Overwrite<T, U> = DistributiveOmit<T, keyof U> & U;
10+
export type Overwrite<T, U> = DistributiveOmit<T, keyof U> & U;
11+
12+
type GenerateStringUnion<T> = Extract<
13+
{
14+
[Key in keyof T]: true extends T[Key] ? Key : never;
15+
}[keyof T],
16+
string
17+
>;
1118

1219
/**
1320
* Generate a set of string literal types with the given default record `T` and
@@ -16,6 +23,6 @@ declare type Overwrite<T, U> = DistributiveOmit<T, keyof U> & U;
1623
* If the property value was `true`, the property key will be added to the
1724
* string union.
1825
*/
19-
declare type OverridableStringUnion<T extends string | number, U = any> = GenerateStringUnion<
26+
export type OverridableStringUnion<T extends string | number, U = any> = GenerateStringUnion<
2027
Overwrite<Record<T, true>, U>
2128
>;

packages/react/src/utils/create-breakpoints.ts

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { OverridableStringUnion } from '../types';
2+
13
export interface BreakpointOverrides {}
24

35
export type Breakpoint = OverridableStringUnion<
@@ -96,7 +98,7 @@ export const breakpointKeys = ['xs', 'sm', 'md', 'lg', 'xl'];
9698
const sortBreakpointsValues = (values: Breakpoints['values']) => {
9799
const breakpointsAsArray = Object.keys(values).map(key => ({
98100
key,
99-
val: values[key],
101+
val: values[key as Breakpoint],
100102
})) || [];
101103

102104
// Sort in ascending order
@@ -109,7 +111,7 @@ const sortBreakpointsValues = (values: Breakpoints['values']) => {
109111
...acc,
110112
[obj.key]: obj.val,
111113
};
112-
}, {});
114+
}, {} as Breakpoints['values']);
113115
};
114116

115117
// Keep in mind that @media is inclusive by the CSS specification.
@@ -133,29 +135,42 @@ export default function createBreakpoints(breakpoints: BreakpointsOptions): Brea
133135
...other
134136
} = breakpoints;
135137
const sortedValues = sortBreakpointsValues(values);
136-
const keys = Object.keys(sortedValues);
138+
const keys = Object.keys(sortedValues) as Breakpoint[];
137139

138140
function up(key: Breakpoint | number) {
139-
const value = typeof values[key] === 'number' ? values[key] : key;
141+
const value = typeof values[key as Breakpoint] === 'number'
142+
? values[key as Breakpoint]
143+
: key;
140144
return `@media (min-width:${value}${unit})`;
141145
}
142146

143147
function down(key: Breakpoint | number) {
144-
const value = typeof values[key] === 'number' ? values[key] : key;
148+
const value = typeof values[key as Breakpoint] === 'number'
149+
? values[key as Breakpoint]
150+
: key as number;
145151
return `@media (max-width:${value - step / 100}${unit})`;
146152
}
147153

148154
function between(start: Breakpoint | number, end: Breakpoint | number) {
149-
const endIndex = keys.indexOf(end);
155+
const endIndex = keys.indexOf(end as Breakpoint);
156+
150157
return (
151-
`@media (min-width:${typeof values[start] === 'number' ? values[start] : start}${unit}) and `
152-
+ `(max-width:${(endIndex !== -1 && typeof values[keys[endIndex]] === 'number' ? values[keys[endIndex]] : end) - step / 100}${unit})`
158+
`@media (min-width:${
159+
typeof values[start as Breakpoint] === 'number'
160+
? values[start as Breakpoint]
161+
: start
162+
}${unit}) and `
163+
+ `(max-width:${(
164+
endIndex !== -1 && typeof values[keys[endIndex] as Breakpoint] === 'number'
165+
? values[keys[endIndex] as Breakpoint]
166+
: end as number
167+
) - step / 100}${unit})`
153168
);
154169
}
155170

156171
function only(key: Breakpoint) {
157172
if (keys.indexOf(key) + 1 < keys.length) {
158-
return between(key, keys[keys.indexOf(key) + 1]);
173+
return between(key, keys[keys.indexOf(key) + 1] as Breakpoint);
159174
}
160175
return up(key);
161176
}
@@ -164,12 +179,12 @@ export default function createBreakpoints(breakpoints: BreakpointsOptions): Brea
164179
// handle first and last key separately, for better readability
165180
const keyIndex = keys.indexOf(key);
166181
if (keyIndex === 0) {
167-
return up(keys[1]);
182+
return up(keys[1] as Breakpoint);
168183
}
169184
if (keyIndex === keys.length - 1) {
170-
return down(keys[keyIndex]);
185+
return down(keys[keyIndex] as Breakpoint);
171186
}
172-
return between(key, keys[keys.indexOf(key) + 1]).replace(
187+
return between(key, keys[keys.indexOf(key) + 1] as Breakpoint).replace(
173188
'@media',
174189
'@media not all and',
175190
);

packages/react/tsconfig.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
{
22
"compilerOptions": {
3-
"baseUrl": ".",
4-
"declaration": true,
5-
"pretty": true,
6-
"skipLibCheck": true,
7-
"esModuleInterop": true,
83
"jsx": "react-jsx",
9-
"strict": true,
104
"lib": ["ESNEXT", "DOM", "DOM.Iterable"],
5+
"baseUrl": ".",
6+
"module": "ESNext",
7+
"moduleResolution": "Bundler",
118
// use global types for vite's expect, describe, etc.
129
"types": ["vitest/globals"],
13-
"noImplicitReturns": true,
10+
"strict": true,
1411
"noFallthroughCasesInSwitch": true,
15-
"module": "ESNext",
16-
"moduleResolution": "Bundler"
12+
"noImplicitReturns": true,
13+
"declaration": true,
14+
"pretty": true,
15+
"esModuleInterop": true,
16+
"skipLibCheck": true
1717
},
1818
"exclude": ["node_modules", "dist"]
1919
}

packages/react/tsup.config.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { defineConfig } from 'tsup';
2+
3+
export default defineConfig({
4+
entry: ['src/index.ts'],
5+
dts: true,
6+
outDir: 'dist',
7+
clean: true,
8+
// minify: true,
9+
// sourcemap: true,
10+
format: ['cjs', 'esm'],
11+
treeshake: true,
12+
splitting: false,
13+
cjsInterop: true,
14+
});

0 commit comments

Comments
 (0)