feat: conditional render component
This commit is contained in:
		
							
								
								
									
										22
									
								
								quartz/components/ConditionalRender.tsx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								quartz/components/ConditionalRender.tsx
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,22 @@
 | 
			
		||||
import { QuartzComponent, QuartzComponentConstructor, QuartzComponentProps } from "./types"
 | 
			
		||||
 | 
			
		||||
type ConditionalRenderConfig = {
 | 
			
		||||
  component: QuartzComponent
 | 
			
		||||
  condition: (props: QuartzComponentProps) => boolean
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export default ((config: ConditionalRenderConfig) => {
 | 
			
		||||
  const ConditionalRender: QuartzComponent = (props: QuartzComponentProps) => {
 | 
			
		||||
    if (config.condition(props)) {
 | 
			
		||||
      return <config.component {...props} />
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return null
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  ConditionalRender.afterDOMLoaded = config.component.afterDOMLoaded
 | 
			
		||||
  ConditionalRender.beforeDOMLoaded = config.component.beforeDOMLoaded
 | 
			
		||||
  ConditionalRender.css = config.component.css
 | 
			
		||||
 | 
			
		||||
  return ConditionalRender
 | 
			
		||||
}) satisfies QuartzComponentConstructor<ConditionalRenderConfig>
 | 
			
		||||
@@ -21,6 +21,7 @@ import RecentNotes from "./RecentNotes"
 | 
			
		||||
import Breadcrumbs from "./Breadcrumbs"
 | 
			
		||||
import Comments from "./Comments"
 | 
			
		||||
import Flex from "./Flex"
 | 
			
		||||
import ConditionalRender from "./ConditionalRender"
 | 
			
		||||
 | 
			
		||||
export {
 | 
			
		||||
  ArticleTitle,
 | 
			
		||||
@@ -46,4 +47,5 @@ export {
 | 
			
		||||
  Breadcrumbs,
 | 
			
		||||
  Comments,
 | 
			
		||||
  Flex,
 | 
			
		||||
  ConditionalRender,
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user