-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3c2040f
commit f456fd7
Showing
15 changed files
with
221 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# useSyncExternalStore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
main { | ||
display: flex; | ||
flex-direction: column; | ||
gap: 1rem; | ||
padding: 3rem; | ||
} | ||
.editable-text { | ||
button { | ||
/* remove button styles. Make it look like text */ | ||
background: none; | ||
border: none; | ||
padding: 4px 8px; | ||
font-size: 1.5rem; | ||
font-weight: bold; | ||
} | ||
|
||
input { | ||
/* make it the same size as the button */ | ||
font-size: 1.5rem; | ||
font-weight: bold; | ||
padding: 4px 8px; | ||
border: none; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import * as ReactDOM from 'react-dom/client' | ||
|
||
function NarrowScreenNotifier() { | ||
const isNarrow = false | ||
return isNarrow ? 'You are on a narrow screen' : 'You are on a wide screen' | ||
} | ||
|
||
function App() { | ||
return <NarrowScreenNotifier /> | ||
} | ||
|
||
const rootEl = document.createElement('div') | ||
document.body.append(rootEl) | ||
ReactDOM.createRoot(rootEl).render(<App />) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# useSyncExternalStore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
main { | ||
display: flex; | ||
flex-direction: column; | ||
gap: 1rem; | ||
padding: 3rem; | ||
} | ||
.editable-text { | ||
button { | ||
/* remove button styles. Make it look like text */ | ||
background: none; | ||
border: none; | ||
padding: 4px 8px; | ||
font-size: 1.5rem; | ||
font-weight: bold; | ||
} | ||
|
||
input { | ||
/* make it the same size as the button */ | ||
font-size: 1.5rem; | ||
font-weight: bold; | ||
padding: 4px 8px; | ||
border: none; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { useSyncExternalStore } from 'react' | ||
import * as ReactDOM from 'react-dom/client' | ||
|
||
const mediaQuery = '(max-width: 600px)' | ||
function getSnapshot() { | ||
return window.matchMedia(mediaQuery).matches | ||
} | ||
|
||
function subscribe(callback: () => void) { | ||
const mediaQueryList = window.matchMedia(mediaQuery) | ||
mediaQueryList.addEventListener('change', callback) | ||
return () => { | ||
mediaQueryList.removeEventListener('change', callback) | ||
} | ||
} | ||
|
||
function NarrowScreenNotifier() { | ||
const isNarrow = useSyncExternalStore(subscribe, getSnapshot) | ||
return isNarrow ? 'You are on a narrow screen' : 'You are on a wide screen' | ||
} | ||
|
||
function App() { | ||
return <NarrowScreenNotifier /> | ||
} | ||
|
||
const rootEl = document.createElement('div') | ||
document.body.append(rootEl) | ||
ReactDOM.createRoot(rootEl).render(<App />) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Make Store Utility |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
main { | ||
display: flex; | ||
flex-direction: column; | ||
gap: 1rem; | ||
padding: 3rem; | ||
} | ||
.editable-text { | ||
button { | ||
/* remove button styles. Make it look like text */ | ||
background: none; | ||
border: none; | ||
padding: 4px 8px; | ||
font-size: 1.5rem; | ||
font-weight: bold; | ||
} | ||
|
||
input { | ||
/* make it the same size as the button */ | ||
font-size: 1.5rem; | ||
font-weight: bold; | ||
padding: 4px 8px; | ||
border: none; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { useSyncExternalStore } from 'react' | ||
import * as ReactDOM from 'react-dom/client' | ||
|
||
const mediaQuery = '(max-width: 600px)' | ||
function getSnapshot() { | ||
return window.matchMedia(mediaQuery).matches | ||
} | ||
|
||
function subscribe(callback: () => void) { | ||
const mediaQueryList = window.matchMedia(mediaQuery) | ||
mediaQueryList.addEventListener('change', callback) | ||
return () => { | ||
mediaQueryList.removeEventListener('change', callback) | ||
} | ||
} | ||
|
||
function NarrowScreenNotifier() { | ||
const isNarrow = useSyncExternalStore(subscribe, getSnapshot) | ||
return isNarrow ? 'You are on a narrow screen' : 'You are on a wide screen' | ||
} | ||
|
||
function App() { | ||
return <NarrowScreenNotifier /> | ||
} | ||
|
||
const rootEl = document.createElement('div') | ||
document.body.append(rootEl) | ||
ReactDOM.createRoot(rootEl).render(<App />) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Make Store Utility |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
main { | ||
display: flex; | ||
flex-direction: column; | ||
gap: 1rem; | ||
padding: 3rem; | ||
} | ||
.editable-text { | ||
button { | ||
/* remove button styles. Make it look like text */ | ||
background: none; | ||
border: none; | ||
padding: 4px 8px; | ||
font-size: 1.5rem; | ||
font-weight: bold; | ||
} | ||
|
||
input { | ||
/* make it the same size as the button */ | ||
font-size: 1.5rem; | ||
font-weight: bold; | ||
padding: 4px 8px; | ||
border: none; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { useSyncExternalStore } from 'react' | ||
import * as ReactDOM from 'react-dom/client' | ||
import { makeMediaQueryStore } from './narrow-media-query' | ||
|
||
const narrowScreenStore = makeMediaQueryStore('(max-width: 600px)') | ||
|
||
function NarrowScreenNotifier() { | ||
const isNarrow = useSyncExternalStore( | ||
narrowScreenStore.subscribe, | ||
narrowScreenStore.getSnapshot, | ||
) | ||
return isNarrow ? 'You are on a narrow screen' : 'You are on a wide screen' | ||
} | ||
|
||
function App() { | ||
return <NarrowScreenNotifier /> | ||
} | ||
|
||
const rootEl = document.createElement('div') | ||
document.body.append(rootEl) | ||
ReactDOM.createRoot(rootEl).render(<App />) |
15 changes: 15 additions & 0 deletions
15
exercises/09.sync-external/02.solution/narrow-media-query.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export function makeMediaQueryStore(mediaQuery: string) { | ||
function getSnapshot() { | ||
return window.matchMedia(mediaQuery).matches | ||
} | ||
|
||
function subscribe(callback: () => void) { | ||
const mediaQueryList = window.matchMedia(mediaQuery) | ||
mediaQueryList.addEventListener('change', callback) | ||
return () => { | ||
mediaQueryList.removeEventListener('change', callback) | ||
} | ||
} | ||
|
||
return { subscribe, getSnapshot } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Sync External State |