-
-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[NoSSR] Port the component from legacy Base UI #593
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,31 @@ | ||
'use client'; | ||
import * as React from 'react'; | ||
import { styled } from '@mui/system'; | ||
import { NoSsr } from '@base_ui/react/NoSsr'; | ||
import { Box } from '@mui/system'; | ||
|
||
export default function SimpleNoSsr() { | ||
return ( | ||
<div> | ||
<Box sx={{ p: 2, bgcolor: 'primary.main', color: 'primary.contrastText' }}> | ||
Server and Client | ||
</Box> | ||
<Demo> | ||
<Panel>Server and Client</Panel> | ||
<NoSsr> | ||
<Box | ||
sx={{ p: 2, bgcolor: 'secondary.main', color: 'secondary.contrastText' }} | ||
> | ||
Client only | ||
</Box> | ||
<Panel>Client only</Panel> | ||
</NoSsr> | ||
</div> | ||
</Demo> | ||
); | ||
} | ||
|
||
const Demo = styled('div')` | ||
height: 250px; | ||
display: flex; | ||
flex-direction: column; | ||
justify-content: center; | ||
align-items: center; | ||
overflow: auto; | ||
padding: 8px; | ||
`; | ||
|
||
const Panel = styled('div')` | ||
padding: var(--space-2); | ||
margin-bottom: var(--space-1); | ||
background-color: var(--gray-100); | ||
`; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,31 @@ | ||
'use client'; | ||
import * as React from 'react'; | ||
import { styled } from '@mui/system'; | ||
import { NoSsr } from '@base_ui/react/NoSsr'; | ||
import { Box } from '@mui/system'; | ||
|
||
export default function SimpleNoSsr() { | ||
return ( | ||
<div> | ||
<Box sx={{ p: 2, bgcolor: 'primary.main', color: 'primary.contrastText' }}> | ||
Server and Client | ||
</Box> | ||
<Demo> | ||
<Panel>Server and Client</Panel> | ||
<NoSsr> | ||
<Box | ||
sx={{ p: 2, bgcolor: 'secondary.main', color: 'secondary.contrastText' }} | ||
> | ||
Client only | ||
</Box> | ||
<Panel>Client only</Panel> | ||
</NoSsr> | ||
</div> | ||
</Demo> | ||
); | ||
} | ||
|
||
const Demo = styled('div')` | ||
height: 250px; | ||
display: flex; | ||
flex-direction: column; | ||
justify-content: center; | ||
align-items: center; | ||
overflow: auto; | ||
padding: 8px; | ||
`; | ||
|
||
const Panel = styled('div')` | ||
padding: var(--space-2); | ||
margin-bottom: var(--space-1); | ||
background-color: var(--gray-100); | ||
`; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,6 @@ | ||
<Box sx={{ p: 2, bgcolor: 'primary.main', color: 'primary.contrastText' }}> | ||
Server and Client | ||
</Box> | ||
<NoSsr> | ||
<Box | ||
sx={{ p: 2, bgcolor: 'secondary.main', color: 'secondary.contrastText' }} | ||
> | ||
Client only | ||
</Box> | ||
</NoSsr> | ||
<Demo> | ||
<Panel>Server and Client</Panel> | ||
<NoSsr> | ||
<Panel>Client only</Panel> | ||
</NoSsr> | ||
</Demo> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
--- | ||
productId: base-ui | ||
title: No SSR React component | ||
title: No-SSR React component | ||
description: The No-SSR component defers the rendering of children components from the server to the client. | ||
components: NoSsr | ||
--- | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know which one is more correct TBH. @colmtuite, @atomiks, could you chime in? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Client only as client bundle only or client side only? 😁 |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ | |
import * as React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { exactProp, unstable_useEnhancedEffect as useEnhancedEffect } from '@mui/utils'; | ||
import { NoSsrProps } from './NoSsr.types'; | ||
|
||
/** | ||
* NoSsr purposely removes components from the subject of Server Side Rendering (SSR). | ||
|
@@ -22,7 +21,7 @@ import { NoSsrProps } from './NoSsr.types'; | |
* | ||
* - [NoSsr API](https://base-ui.netlify.app/components/react-no-ssr/#api-reference-NoSsr) | ||
*/ | ||
function NoSsr(props: NoSsrProps): JSX.Element { | ||
function NoSsr(props: NoSsr.Props): JSX.Element { | ||
const { children, defer = false, fallback = null } = props; | ||
const [mountedState, setMountedState] = React.useState(false); | ||
|
||
|
@@ -42,13 +41,33 @@ function NoSsr(props: NoSsrProps): JSX.Element { | |
return <React.Fragment>{mountedState ? children : fallback}</React.Fragment>; | ||
} | ||
|
||
namespace NoSsr { | ||
export interface Props { | ||
Comment on lines
+44
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a new pattern? How can people extend the NoSsr prop now? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, we decided to go with this pattern. See the rationale in #517. The interface should be extendable the same way as without the namespace. |
||
/** | ||
* React node to render on client only. | ||
*/ | ||
children?: React.ReactNode; | ||
/** | ||
* If `true`, the component will not only prevent server-side rendering. | ||
* It will also defer the rendering of the children into a different screen frame. | ||
* @default false | ||
*/ | ||
defer?: boolean; | ||
/** | ||
* The fallback content to display. | ||
* @default null | ||
*/ | ||
fallback?: React.ReactNode; | ||
} | ||
} | ||
|
||
NoSsr.propTypes /* remove-proptypes */ = { | ||
// ┌────────────────────────────── Warning ──────────────────────────────┐ | ||
// │ These PropTypes are generated from the TypeScript type definitions. │ | ||
// │ To update them, edit the TypeScript types and run `pnpm proptypes`. │ | ||
// └─────────────────────────────────────────────────────────────────────┘ | ||
/** | ||
* You can wrap a node. | ||
* React node to render on client only. | ||
*/ | ||
children: PropTypes.node, | ||
/** | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
export { NoSsr } from './NoSsr'; | ||
export * from './NoSsr.types'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, I love how it makes the
.preview
much easier to follow.