Skip to content
This repository was archived by the owner on Apr 14, 2020. It is now read-only.

Commit 89b984f

Browse files
committed
chore: reduce bundle size
1 parent 1b57247 commit 89b984f

4 files changed

Lines changed: 137 additions & 135 deletions

File tree

packages/yubaba/src/Collector/index.tsx

Lines changed: 103 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -104,113 +104,111 @@ export interface Collect {
104104

105105
export const CollectorContext = React.createContext<Collect | undefined>(undefined);
106106

107-
export default class Collector extends React.Component<CollectorProps> {
108-
render() {
109-
const {
110-
children,
111-
style,
112-
className,
113-
data,
114-
receiveRenderChildren,
115-
receiveRef,
116-
receiveData,
117-
topMostCollector,
118-
} = this.props;
119-
120-
if (typeof children !== 'function') {
121-
return (
122-
<CollectorContext.Consumer>
123-
{collect => (
124-
<CollectorContext.Provider
125-
value={{
126-
ref: ref => {
127-
if (receiveRef) {
128-
receiveRef(ref);
129-
}
130-
131-
if (!topMostCollector && collect) {
132-
collect.ref(ref);
133-
}
134-
},
135-
focalTargetRef: ref => {
136-
const { receiveFocalTargetRef } = this.props;
137-
if (receiveFocalTargetRef) {
138-
receiveFocalTargetRef(ref);
139-
}
140-
141-
if (!topMostCollector && collect) {
142-
collect.focalTargetRef(ref);
143-
}
144-
},
145-
data: childData => {
146-
const collectedData = data ? [data].concat(childData) : childData;
147-
if (!topMostCollector && collect) {
148-
collect.data(collectedData);
149-
}
150-
151-
if (receiveData) {
152-
receiveData(childData);
153-
}
154-
},
155-
renderChildren: node => {
156-
if (!topMostCollector && collect) {
157-
collect.renderChildren(node);
158-
}
159-
160-
if (receiveRenderChildren) {
161-
receiveRenderChildren(node);
162-
}
163-
},
164-
style: {
165-
...style,
166-
...(collect && !topMostCollector ? collect.style : {}),
167-
},
168-
className:
169-
className || (collect && !topMostCollector ? collect.className : undefined),
170-
}}
171-
>
172-
{children}
173-
</CollectorContext.Provider>
174-
)}
175-
</CollectorContext.Consumer>
176-
);
177-
}
178-
107+
const Collector: React.FC<CollectorProps> = ({
108+
children,
109+
style,
110+
className,
111+
data,
112+
receiveRenderChildren,
113+
receiveRef,
114+
receiveData,
115+
topMostCollector,
116+
receiveFocalTargetRef,
117+
}: CollectorProps) => {
118+
if (typeof children !== 'function') {
179119
return (
180120
<CollectorContext.Consumer>
181-
{collect => {
182-
if (typeof children === 'function') {
183-
if (!topMostCollector && collect) {
184-
const collectedData = data ? [data] : [];
185-
collect.renderChildren(children);
186-
collect.data(collectedData);
187-
}
188-
189-
if (receiveRenderChildren) {
190-
receiveRenderChildren(children);
191-
}
192-
193-
return React.Children.only(
194-
children({
195-
className:
196-
className || (collect && !topMostCollector ? collect.className : undefined),
197-
ref: (ref: HTMLElement) => {
198-
if (!topMostCollector && collect) {
199-
collect.ref(ref);
200-
}
201-
202-
if (receiveRef) {
203-
receiveRef(ref);
204-
}
205-
},
206-
style: collect && !topMostCollector ? { ...style, ...collect.style } : style || {},
207-
})
208-
);
209-
}
210-
211-
throw new Error('Children is guaranteed to be a function. Impossible condition.');
212-
}}
121+
{collect => (
122+
<CollectorContext.Provider
123+
value={{
124+
ref: ref => {
125+
if (receiveRef) {
126+
receiveRef(ref);
127+
}
128+
129+
if (!topMostCollector && collect) {
130+
collect.ref(ref);
131+
}
132+
},
133+
focalTargetRef: ref => {
134+
if (receiveFocalTargetRef) {
135+
receiveFocalTargetRef(ref);
136+
}
137+
138+
if (!topMostCollector && collect) {
139+
collect.focalTargetRef(ref);
140+
}
141+
},
142+
data: childData => {
143+
const collectedData = data ? [data].concat(childData) : childData;
144+
if (!topMostCollector && collect) {
145+
collect.data(collectedData);
146+
}
147+
148+
if (receiveData) {
149+
receiveData(childData);
150+
}
151+
},
152+
renderChildren: node => {
153+
if (!topMostCollector && collect) {
154+
collect.renderChildren(node);
155+
}
156+
157+
if (receiveRenderChildren) {
158+
receiveRenderChildren(node);
159+
}
160+
},
161+
style: {
162+
...style,
163+
...(collect && !topMostCollector ? collect.style : {}),
164+
},
165+
className:
166+
className || (collect && !topMostCollector ? collect.className : undefined),
167+
}}
168+
>
169+
{children}
170+
</CollectorContext.Provider>
171+
)}
213172
</CollectorContext.Consumer>
214173
);
215174
}
216-
}
175+
176+
return (
177+
<CollectorContext.Consumer>
178+
{collect => {
179+
if (typeof children === 'function') {
180+
if (!topMostCollector && collect) {
181+
const collectedData = data ? [data] : [];
182+
collect.renderChildren(children);
183+
collect.data(collectedData);
184+
}
185+
186+
if (receiveRenderChildren) {
187+
receiveRenderChildren(children);
188+
}
189+
190+
return React.Children.only(
191+
children({
192+
className:
193+
className || (collect && !topMostCollector ? collect.className : undefined),
194+
ref: (ref: HTMLElement) => {
195+
if (!topMostCollector && collect) {
196+
collect.ref(ref);
197+
}
198+
199+
if (receiveRef) {
200+
receiveRef(ref);
201+
}
202+
},
203+
style: collect && !topMostCollector ? { ...style, ...collect.style } : style || {},
204+
})
205+
);
206+
}
207+
208+
return null;
209+
}}
210+
</CollectorContext.Consumer>
211+
);
212+
};
213+
214+
export default Collector;

packages/yubaba/src/FocalTarget/index.tsx

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@ interface FocalTargetProps {
1010
children: (props: FocalTargetChildProps) => React.ReactNode;
1111
}
1212

13-
export default class FocalTarget extends React.Component<FocalTargetProps> {
14-
render() {
15-
return (
16-
<CollectorContext.Consumer>
17-
{collect => {
18-
return this.props.children({ ref: collect ? collect.focalTargetRef : noop });
19-
}}
20-
</CollectorContext.Consumer>
21-
);
22-
}
23-
}
13+
const FocalTarget: React.FC<FocalTargetProps> = (props: FocalTargetProps) => (
14+
<CollectorContext.Consumer>
15+
{collect => {
16+
return props.children({ ref: collect ? collect.focalTargetRef : noop });
17+
}}
18+
</CollectorContext.Consumer>
19+
);
20+
21+
export default FocalTarget;

packages/yubaba/src/Wait/index.tsx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import * as React from 'react';
2-
import Collector, { CollectorChildrenProps, CollectorActions, CollectorData } from '../Collector';
2+
import Collector, { CollectorChildrenProps, CollectorActions } from '../Collector';
33

4-
export default class Wait extends React.Component<CollectorChildrenProps> {
5-
data: CollectorData = {
6-
action: CollectorActions.wait,
7-
};
4+
const Wait: React.FC<CollectorChildrenProps> = (props: CollectorChildrenProps) => (
5+
<Collector
6+
data={{
7+
action: CollectorActions.wait,
8+
}}
9+
>
10+
{props.children}
11+
</Collector>
12+
);
813

9-
render() {
10-
return <Collector data={this.data}>{this.props.children}</Collector>;
11-
}
12-
}
14+
export default Wait;
Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
import { CollectorData, ElementData } from '../Collector';
22

3-
43
export interface BabaData {
54
elementData: ElementData;
65
collectorData: CollectorData[];
76
}
87

8+
interface BabaStore {
9+
[key: string]: BabaData;
10+
}
911

10-
const store = new Map<string, BabaData>();
11-
12-
13-
export const set = (key: string, value: BabaData) => store.set(key, value);
14-
15-
16-
export const get = (key: string) => store.get(key);
12+
const store: BabaStore = {};
1713

14+
export const set = (key: string, value: BabaData) => {
15+
store[key] = value;
16+
};
1817

19-
export const has = (key: string) => store.has(key);
18+
export const get = (key: string): BabaData => {
19+
return store[key];
20+
};
2021

22+
export const has = (key: string): boolean => !!get(key);
2123

22-
export const remove = (key: string) => store.delete(key);
24+
export const remove = (key: string) => {
25+
delete store[key];
26+
};

0 commit comments

Comments
 (0)