File tree Expand file tree Collapse file tree 2 files changed +38
-46
lines changed
packages/react/src/internal Expand file tree Collapse file tree 2 files changed +38
-46
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1+ /**
2+ * Copyright IBM Corp. 2016, 2025
3+ *
4+ * This source code is licensed under the Apache-2.0 license found in the
5+ * LICENSE file in the root directory of this source tree.
6+ */
7+
8+ import { useState , useEffect } from 'react' ;
9+ import { canUseDOM } from './environment' ;
10+
11+ /** Listens to changes in a media query and returns whether it matches. */
12+ export const useMatchMedia = ( mediaQuery : string ) => {
13+ const [ matches , setMatches ] = useState ( ( ) => {
14+ if ( canUseDOM ) {
15+ const mediaQueryList = window . matchMedia ( mediaQuery ) ;
16+ return mediaQueryList . matches ;
17+ }
18+ return false ;
19+ } ) ;
20+
21+ useEffect ( ( ) => {
22+ const listener = ( event : MediaQueryListEvent ) => {
23+ setMatches ( event . matches ) ;
24+ } ;
25+
26+ const mediaQueryList = window . matchMedia ( mediaQuery ) ;
27+
28+ mediaQueryList . addEventListener ( 'change' , listener ) ;
29+
30+ setMatches ( mediaQueryList . matches ) ;
31+
32+ return ( ) => {
33+ mediaQueryList . removeEventListener ( 'change' , listener ) ;
34+ } ;
35+ } , [ mediaQuery ] ) ;
36+
37+ return matches ;
38+ } ;
You can’t perform that action at this time.
0 commit comments