Skip to content

Commit 128d926

Browse files
author
Andrii Kirmas
committed
Remove old contexted implementation
1 parent e822d77 commit 128d926

File tree

7 files changed

+37
-225
lines changed

7 files changed

+37
-225
lines changed

__recipes__/cra/src/App.tsx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Component, LinkHTMLAttributes, PropsWithChildren } from "react";
2-
import classNaming from "react-classnaming"
2+
import {classNamingBasic, classNamingCtx} from "react-classnaming"
33
import type { ClassNames } from "react-classnaming"
44

55
type AppProps = PropsWithChildren<
@@ -16,19 +16,21 @@ function App({
1616
App__Container
1717
}
1818
}: AppProps) {
19-
const classes = classNaming(classNames)
19+
const classes = classNamingCtx({classNames}, {withClassNames: true})
2020

2121
return (
22-
<div {...classNaming(className, {App__Container})} id={classNaming<string>({App__Container})}>
22+
<div
23+
{...classNamingBasic(className, {App__Container})}
24+
id={classNamingBasic<string>({App__Container})}
25+
>
2326
<Header
2427
// TODO Why TS doesn't check object
2528
{...classes({App__Header: true})}
26-
{...{classNames}}
2729
//@ts-expect-error Property 'className' does not exist
2830
className=""
2931
/>
3032
<Content {...{
31-
...classes("App__Content"),
33+
...classes(),
3234
classNames
3335
}}>
3436
<Link {...{classNames}} href="https://reactjs.org">
@@ -44,7 +46,7 @@ function App({
4446
type LinkProps = ClassNames<"App__link"> & PropsWithChildren<LinkHTMLAttributes<HTMLLinkElement>>
4547
function Link({href, children, "classNames": {App__link}}: LinkProps) {
4648
return <a {...{
47-
...classNaming({App__link}),
49+
...classNamingBasic({App__link}),
4850
href,
4951
"target": "_blank",
5052
"rel": "noopener noreferrer"
@@ -54,7 +56,7 @@ function Link({href, children, "classNames": {App__link}}: LinkProps) {
5456
}
5557

5658
function Header({classNames: {Header}}: ClassNames<"Header">) {
57-
return <header {...classNaming({Header})}>Header</header>
59+
return <header {...classNamingBasic({Header})}>Header</header>
5860
}
5961
class Content extends Component<PropsWithChildren<ClassNames<true, "Content">>> {
6062
render() {
@@ -64,7 +66,7 @@ class Content extends Component<PropsWithChildren<ClassNames<true, "Content">>>
6466
children
6567
} = this.props
6668

67-
return <main {...classNaming(className, {Content})}>{children}</main>
69+
return <main {...classNamingBasic(className, {Content})}>{children}</main>
6870
}
6971
}
7072

src/ctx.test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,12 @@ describe(classNamingCtx.name, () => {
8989
}))
9090
})
9191

92-
it("for component", () => expect(classNamingCtx(
92+
it("only propagate classNames", () => expect(classNamingCtx(
9393
{classNames},
9494
{withClassNames: true}
95-
)(
96-
false
97-
)).toStrictEqual({
95+
)()).toStrictEqual({
9896
className: "",
9997
classNames
10098
}))
99+
101100
})

src/ctx.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ interface tClassNaming<
1616
*/
1717
// Using overloads will make error not in certain argument but on all call - 'No overload found'
1818
(
19-
propagate_or_map_or_expression: true | ToggleMap<ClassKeys> | ClassKeys | Falsy,
19+
propagate_or_map_or_expression?: true | ToggleMap<ClassKeys> | ClassKeys | Falsy,
2020
map_or_expression?: (
2121
[Extract<typeof propagate_or_map_or_expression, true>] extends [never]
2222
? never
@@ -59,7 +59,7 @@ function classNamer<
5959
withClassNames extends boolean|undefined
6060
>(
6161
this: Partial<ClassNamer<ClassKeys> & ClassNamerOptions<withClassNames>>,
62-
arg0: true | ToggleMap<ClassKeys> | ClassKeys,
62+
arg0?: true | ToggleMap<ClassKeys> | ClassKeys,
6363
arg1?: ToggleMap<ClassKeys> | ClassKeys,
6464
...args: (ClassKeys | Falsy)[]
6565
): ClassNamed & Partial<Pick<typeof this, "classNames">> {
@@ -71,7 +71,6 @@ function classNamer<
7171
} = this
7272
, withPropagation = arg0 === true
7373
, allowed: ClassKeys[] = truthyKeys(arg0 === true ? false : arg0)
74-
//@ts-expect-error
7574
.concat(truthyKeys(arg1))
7675
//@ts-expect-error
7776
.concat(args)

src/defs.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
11
import type { JSXElementConstructor } from "react"
2-
export interface ClassToggling<K extends string> {
3-
/**
4-
* @example <div {...classToggling({class1: !isHidden}, isOpen2 && class2)} />
5-
*/
6-
(toggleMapOrKeyExpression: Falsy|K|ToggleMap<K>, ...classKeyExpressions: (Falsy|K)[]): ClassNamed
7-
//TODO (withClassName: true|false, ...toggles: K[]): tClassNamed
8-
}
9-
10-
export interface ClassNaming<K extends string> extends ClassNamed, ClassToggling<K> {}
112

123
/** Multipurpose generic
134
* @example ClassNames<true> === {className: string}
145
* @example ClassNames<"class1"|"class2"> === {classNames: {class1: undefined|string, class2: undefined|string}}
156
* @example ClassNames<Props1, Props2> === {classNames: Props1["classNames"] & Props2["classNames"]}
167
* @example ClassNames<true, "class1", Props, typeof Component1, typeof FunctionalComponent>
178
*/
18-
//TODO string | ClassNamesMap
9+
//TODO Consider string | ClassNamesMap
1910
export type ClassNames<
2011
C0 extends true | string| ReactRelated,
2112
C1 extends (C0 extends true ? string : never) | ReactRelated = never,

src/index.spec.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ function Root(_: ClassNames<"fc1"|"fc2">) {
88
}
99

1010
describe(classNamingBasic.name, () => {
11-
function Button({
12-
className, "classNames": { Btn }
13-
}: ClassNames<true, "Btn">) {
11+
function Button({className, "classNames": { Btn }}: ClassNames<true, "Btn">) {
1412
return <button {...classNamingBasic(className, { Btn })}/>
1513
}
1614

@@ -29,14 +27,26 @@ describe(classNamingBasic.name, () => {
2927
</>
3028
}
3129

32-
it("demo", () => expectToRender(
30+
it("not css module", () => expectToRender(
3331
<Root classNames={classNamesCheck()}/>,
3432
[
3533
'<button class="App__Item Btn"></button>',
3634
'<div class="App__Footer" data-class="App__Footer"></div>'
3735
]
3836
))
3937

38+
it("css module", () => expectToRender(
39+
<Root classNames={{
40+
App__Footer: "footer-hash",
41+
App__Item: "item-hash",
42+
Btn: "btn-hash"
43+
}}/>,
44+
[
45+
'<button class="item-hash btn-hash"></button>',
46+
'<div class="footer-hash" data-class="footer-hash"></div>'
47+
]
48+
))
49+
4050
it("vscode renamed", () => {
4151
function Root({
4252
"classNames": {App: App__Container}

src/index.test.ts

Lines changed: 0 additions & 135 deletions
This file was deleted.

0 commit comments

Comments
 (0)