File tree Expand file tree Collapse file tree 2 files changed +31
-1
lines changed
packages/@react-aria/utils Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Original file line number Diff line number Diff line change 11
11
*/
12
12
13
13
import { useEffect , useState } from 'react' ;
14
+ import { useIsSSR } from '@react-aria/ssr' ;
14
15
15
16
interface ViewportSize {
16
17
width : number ,
@@ -21,7 +22,8 @@ interface ViewportSize {
21
22
let visualViewport = typeof document !== 'undefined' && window . visualViewport ;
22
23
23
24
export function useViewportSize ( ) : ViewportSize {
24
- let [ size , setSize ] = useState ( ( ) => getViewportSize ( ) ) ;
25
+ let isSSR = useIsSSR ( ) ;
26
+ let [ size , setSize ] = useState ( ( ) => isSSR ? { width : 0 , height : 0 } : getViewportSize ( ) ) ;
25
27
26
28
useEffect ( ( ) => {
27
29
// Use visualViewport api to track available height even on iOS virtual keyboard opening
Original file line number Diff line number Diff line change
1
+ /*
2
+ * Copyright 2020 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ import { testSSR } from '@react-spectrum/test-utils' ;
14
+
15
+ describe ( 'useViewportSize SSR' , ( ) => {
16
+ it ( 'should render without errors' , async ( ) => {
17
+ await testSSR ( __filename , `
18
+ import {useViewportSize} from '../src';
19
+
20
+ function Viewport() {
21
+ useViewportSize();
22
+ return null;
23
+ }
24
+
25
+ <Viewport />
26
+ ` ) ;
27
+ } ) ;
28
+ } ) ;
You can’t perform that action at this time.
0 commit comments