@basiln/utils 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- package/.eslintrc.json +4 -0
- package/.stylelintrc +3 -0
- package/CHANGELOG.md +17 -0
- package/LICENSE +21 -0
- package/package.json +49 -0
- package/src/Choose.tsx +37 -0
- package/src/If.tsx +8 -0
- package/src/composeEventHandlers.ts +18 -0
- package/src/createContext.tsx +40 -0
- package/src/getVar.ts +9 -0
- package/src/index.ts +5 -0
- package/src/types/Choose.ts +14 -0
- package/src/types/If.ts +6 -0
- package/src/types/getVar.ts +1 -0
- package/src/types/index.ts +9 -0
- package/tsconfig.json +13 -0
- package/tsup.config.ts +13 -0
package/.eslintrc.json
ADDED
package/.stylelintrc
ADDED
package/CHANGELOG.md
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# Change Log
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
5
|
+
|
6
|
+
# 0.1.0 (2024-11-21)
|
7
|
+
|
8
|
+
|
9
|
+
### Bug Fixes
|
10
|
+
|
11
|
+
* **util:** build error fix ([cbb6bba](https://github.com/seedn-corp/basiln-packages-fe/commit/cbb6bbaf34ecfa267109961dd9d6853c93a0631f))
|
12
|
+
|
13
|
+
|
14
|
+
### Features
|
15
|
+
|
16
|
+
* tsup 세팅 ([c6e1aaf](https://github.com/seedn-corp/basiln-packages-fe/commit/c6e1aaf2f9b0ba6ef2090531bdcbe466581723d4))
|
17
|
+
* 컴포넌트 추가 ([3656d2f](https://github.com/seedn-corp/basiln-packages-fe/commit/3656d2f8c58a14a3fd7931c6349c34b49fce676c))
|
package/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2024 SeedN
|
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.
|
package/package.json
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
{
|
2
|
+
"name": "@basiln/utils",
|
3
|
+
"version": "0.1.0",
|
4
|
+
"exports": {
|
5
|
+
".": "./src/index.ts",
|
6
|
+
"./package.json": "./package.json"
|
7
|
+
},
|
8
|
+
"main": "./src/index.ts",
|
9
|
+
"publishConfig": {
|
10
|
+
"access": "public",
|
11
|
+
"exports": {
|
12
|
+
".": {
|
13
|
+
"types": "./dist/index.d.ts",
|
14
|
+
"require": "./dist/index.js",
|
15
|
+
"import": "./dist/index.mjs"
|
16
|
+
},
|
17
|
+
"./package.json": "./package.json"
|
18
|
+
},
|
19
|
+
"main": "./dist/index.js"
|
20
|
+
},
|
21
|
+
"license": "ISC",
|
22
|
+
"scripts": {
|
23
|
+
"build": "tsup",
|
24
|
+
"dev": "yarn run build --watch",
|
25
|
+
"typecheck": "tsc --noEmit"
|
26
|
+
},
|
27
|
+
"devDependencies": {
|
28
|
+
"@types/eslint": "^9.6.1",
|
29
|
+
"@types/node": "^22.9.1",
|
30
|
+
"@types/react": "^18.3.12",
|
31
|
+
"@types/react-dom": "^18.3.1",
|
32
|
+
"@typescript-eslint/eslint-plugin": "^5.49.0",
|
33
|
+
"@typescript-eslint/parser": "^5.49.0",
|
34
|
+
"eslint": "^8.33.0",
|
35
|
+
"eslint-config-prettier": "^8.6.0",
|
36
|
+
"eslint-import-resolver-typescript": "^3.6.3",
|
37
|
+
"eslint-plugin-import": "^2.31.0",
|
38
|
+
"eslint-plugin-no-relative-import-paths": "^1.5.5",
|
39
|
+
"eslint-plugin-react-hooks": "^5.0.0",
|
40
|
+
"eslint-plugin-react-refresh": "^0.4.14",
|
41
|
+
"tsup": "^6.5.0",
|
42
|
+
"typescript": "^4.9.5"
|
43
|
+
},
|
44
|
+
"peerDependencies": {
|
45
|
+
"react": "^18",
|
46
|
+
"react-dom": "^18"
|
47
|
+
},
|
48
|
+
"gitHead": "dfa7ad216f0cd4359b92e63301de82980780f765"
|
49
|
+
}
|
package/src/Choose.tsx
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
import React, { isValidElement, type FC } from 'react';
|
2
|
+
import type {
|
3
|
+
ChooseOtherwiseProps,
|
4
|
+
ChooseProps,
|
5
|
+
ChooseWhenProps,
|
6
|
+
} from './types';
|
7
|
+
|
8
|
+
export function Choose({ children }: ChooseProps) {
|
9
|
+
const validChildren = React.Children.toArray(children);
|
10
|
+
|
11
|
+
const matchingChild = validChildren.find(
|
12
|
+
child =>
|
13
|
+
isValidElement<ChooseWhenProps>(child) &&
|
14
|
+
child.type === Choose.When &&
|
15
|
+
child.props.condition,
|
16
|
+
);
|
17
|
+
|
18
|
+
if (matchingChild) {
|
19
|
+
return matchingChild;
|
20
|
+
}
|
21
|
+
|
22
|
+
const otherwiseChild = validChildren.find(
|
23
|
+
child => React.isValidElement(child) && child.type === Choose.Otherwise,
|
24
|
+
);
|
25
|
+
|
26
|
+
return otherwiseChild || <></>;
|
27
|
+
}
|
28
|
+
|
29
|
+
const ChooseWhen: FC<ChooseWhenProps> = ({ children }) => <>{children}</>;
|
30
|
+
ChooseWhen.displayName = 'Choose.When';
|
31
|
+
|
32
|
+
const ChooseOtherwise: FC<ChooseOtherwiseProps> = ({ children }) => (
|
33
|
+
<>{children}</>
|
34
|
+
);
|
35
|
+
|
36
|
+
Choose.When = ChooseWhen;
|
37
|
+
Choose.Otherwise = ChooseOtherwise;
|
package/src/If.tsx
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
// @see https://github.com/radix-ui/primitives/blob/main/packages/core/primitive/src/primitive.tsx
|
2
|
+
|
3
|
+
export function composeEventHandlers<E>(
|
4
|
+
originalEventHandler?: (event: E) => void,
|
5
|
+
ourEventHandler?: (event: E) => void,
|
6
|
+
{ checkForDefaultPrevented = true } = {},
|
7
|
+
) {
|
8
|
+
return function handleEvent(event: E) {
|
9
|
+
originalEventHandler?.(event);
|
10
|
+
|
11
|
+
if (
|
12
|
+
checkForDefaultPrevented === false ||
|
13
|
+
!(event as unknown as Event).defaultPrevented
|
14
|
+
) {
|
15
|
+
return ourEventHandler?.(event);
|
16
|
+
}
|
17
|
+
};
|
18
|
+
}
|
@@ -0,0 +1,40 @@
|
|
1
|
+
import React, {
|
2
|
+
useMemo,
|
3
|
+
createContext as createContextRaw,
|
4
|
+
useContext as useContextRaw,
|
5
|
+
type PropsWithChildren,
|
6
|
+
} from 'react';
|
7
|
+
|
8
|
+
export function createContext<ContextValueType extends object | null>(
|
9
|
+
rootComponentName: string,
|
10
|
+
defaultContext?: ContextValueType,
|
11
|
+
) {
|
12
|
+
const Context = createContextRaw<ContextValueType | undefined>(
|
13
|
+
defaultContext,
|
14
|
+
);
|
15
|
+
|
16
|
+
function Provider(props: PropsWithChildren<ContextValueType>) {
|
17
|
+
const { children, ...contextValues } = props;
|
18
|
+
|
19
|
+
const value = useMemo(
|
20
|
+
() => contextValues,
|
21
|
+
[contextValues],
|
22
|
+
) as ContextValueType;
|
23
|
+
|
24
|
+
return <Context.Provider value={value}>{children}</Context.Provider>;
|
25
|
+
}
|
26
|
+
|
27
|
+
function useContext(consumerName: string) {
|
28
|
+
const context = useContextRaw(Context);
|
29
|
+
if (context == null) {
|
30
|
+
throw new Error(
|
31
|
+
`${consumerName}은 ${rootComponentName}하위에서 사용해야 합니다.`,
|
32
|
+
);
|
33
|
+
}
|
34
|
+
|
35
|
+
return context;
|
36
|
+
}
|
37
|
+
|
38
|
+
Provider.displayName = `${rootComponentName}Provider`;
|
39
|
+
return [Provider, useContext] as const;
|
40
|
+
}
|
package/src/getVar.ts
ADDED
package/src/index.ts
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
import { ReactNode } from 'react';
|
2
|
+
|
3
|
+
export type ChooseWhenProps = {
|
4
|
+
condition: boolean;
|
5
|
+
children: ReactNode;
|
6
|
+
};
|
7
|
+
|
8
|
+
export type ChooseProps = {
|
9
|
+
children: ReactNode;
|
10
|
+
};
|
11
|
+
|
12
|
+
export type ChooseOtherwiseProps = {
|
13
|
+
children: ReactNode;
|
14
|
+
};
|
package/src/types/If.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export type VariableType = `--${string}`;
|
package/tsconfig.json
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
{
|
2
|
+
"extends": "../../tsconfig.base.json",
|
3
|
+
"compilerOptions": {
|
4
|
+
"jsx": "react-jsx",
|
5
|
+
"resolveJsonModule": true,
|
6
|
+
"isolatedModules": true,
|
7
|
+
"esModuleInterop": true,
|
8
|
+
"declaration": true,
|
9
|
+
"outDir": "./dist"
|
10
|
+
},
|
11
|
+
"include": ["src", "./tsup.config.ts"],
|
12
|
+
"exclude": ["node_modules"]
|
13
|
+
}
|
package/tsup.config.ts
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
import { defineConfig } from "tsup";
|
2
|
+
import pkgJson from "./package.json";
|
3
|
+
|
4
|
+
const external = [...Object.keys((pkgJson as any).peerDependencies || {})];
|
5
|
+
|
6
|
+
export default defineConfig({
|
7
|
+
entry: ["src/**/*.{ts,tsx}"],
|
8
|
+
format: ["esm", "cjs"],
|
9
|
+
sourcemap: true,
|
10
|
+
clean: true,
|
11
|
+
dts: "src/index.ts",
|
12
|
+
external,
|
13
|
+
});
|