Skip to content

Commit d379b3c

Browse files
committed
feat: allow ref forwarding
1 parent 4dbe720 commit d379b3c

File tree

6 files changed

+49
-36
lines changed

6 files changed

+49
-36
lines changed

example/app.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ export default class App extends Component <{}, AppState> {
5757
shards={[this.scrollRef]}
5858
onActivation={() => console.log("activated")}
5959
onDeactivation={() => console.log("deactivated")}
60+
61+
className="test-class-name"
6062
>
6163
Holala!! {on ? "on" : "off"}
6264
<button onClick={() => alert('ok')}>test inside event</button>

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"react-focus-lock": "^2.1.1",
4545
"react-remove-scroll": "^2.0.4",
4646
"react-style-singleton": "^2.0.0",
47+
"use-callback-ref": "^1.1.0",
4748
"use-sidecar": "^1.0.1"
4849
},
4950
"engines": {

src/Combination.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ const RequireSideCar = (props: any) => {
77
return <SideCar {...props} />;
88
};
99

10-
export function FocusOn(props: ReactFocusOnProps) {
11-
return (
12-
<ReactFocusOn
13-
{...props}
14-
sideCar={RequireSideCar}
15-
/>
16-
);
17-
}
10+
export const FocusOn = React.forwardRef<HTMLElement, ReactFocusOnProps>((props, ref) => (
11+
<ReactFocusOn
12+
{...props}
13+
ref={ref}
14+
sideCar={RequireSideCar}
15+
/>
16+
));

src/UI.tsx

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,52 @@ import ReactFocusLock from 'react-focus-lock/UI'
77
import {EffectProps, ReactFocusOnSideProps, LockProps} from "./types";
88
import {effectCar} from "./medium";
99

10-
export function FocusOn(props: ReactFocusOnSideProps) {
10+
export const FocusOn = React.forwardRef<HTMLElement, ReactFocusOnSideProps>((props, parentRef) => {
1111
const [lockProps, setLockProps] = React.useState<LockProps>(false as any);
1212

13-
const {children, autoFocus, shards, enabled = true, scrollLock = true, focusLock = true, returnFocus = true, inert, sideCar, ...rest} = props;
13+
const {children, autoFocus, shards, enabled = true, scrollLock = true, focusLock = true, returnFocus = true, inert, sideCar, className, ...rest} = props;
1414

1515
const SideCar: SideCarComponent<EffectProps> = sideCar;
1616

1717
const {onActivation, onDeactivation, ...restProps} = lockProps;
1818

1919
return (
2020
<>
21-
<RemoveScroll
21+
<ReactFocusLock
22+
ref={parentRef}
23+
2224
sideCar={sideCar}
23-
enabled={enabled && scrollLock}
25+
disabled={!(lockProps && enabled && focusLock)}
26+
27+
returnFocus={returnFocus}
28+
autoFocus={autoFocus}
29+
2430
shards={shards}
25-
inert={inert}
31+
32+
onActivation={onActivation}
33+
onDeactivation={onDeactivation}
34+
35+
className={className}
36+
37+
as={RemoveScroll}
38+
lockProps={{
39+
...restProps,
40+
sideCar,
41+
shards,
42+
inert,
43+
enabled: enabled && scrollLock,
44+
}}
2645
>
27-
{enabled && <SideCar
28-
{...rest}
29-
sideCar={effectCar}
30-
setLockProps={setLockProps}
31-
shards={shards}
32-
/>}
33-
<ReactFocusLock
34-
sideCar={sideCar}
35-
disabled={!(lockProps && enabled && focusLock)}
36-
37-
returnFocus={returnFocus}
38-
autoFocus={autoFocus}
39-
40-
shards={shards}
41-
42-
lockProps={restProps}
43-
onActivation={onActivation}
44-
onDeactivation={onDeactivation}
45-
>
46-
{children}
47-
</ReactFocusLock>
48-
</RemoveScroll>
46+
{children}
47+
</ReactFocusLock>
48+
{enabled && <SideCar
49+
{...rest}
50+
sideCar={effectCar}
51+
setLockProps={setLockProps}
52+
shards={shards}
53+
/>}
4954
</>
5055
);
51-
}
56+
})
5257

5358
export * from './reExports';

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export interface ReactFocusOnProps extends CommonProps {
3131
autoFocus?: boolean;
3232
returnFocus?: boolean | FocusOptions;
3333

34+
className?: string;
3435
children: React.ReactNode;
3536
}
3637

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9552,6 +9552,11 @@ url@^0.11.0:
95529552
punycode "1.3.2"
95539553
querystring "0.2.0"
95549554

9555+
use-callback-ref@^1.1.0:
9556+
version "1.1.0"
9557+
resolved "https://registry.yarnpkg.com/use-callback-ref/-/use-callback-ref-1.1.0.tgz#e41d2b29aea8a568a6dbefe0af56155eac1b6884"
9558+
integrity sha512-a2j1dVM9UTxdIfZjbdhcD8nCDMDBkTp9rFasSBj3QPcVclYdpKbmLzqLddH3R6WuOff9W6Xo50VedPoVNu8z8A==
9559+
95559560
use-sidecar@^1.0.1:
95569561
version "1.0.1"
95579562
resolved "https://registry.yarnpkg.com/use-sidecar/-/use-sidecar-1.0.1.tgz#75c7a5fdacc14bd3ab64992c638e45a396ad2fad"

0 commit comments

Comments
 (0)