-
-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate data attrs and class name demos
- Loading branch information
1 parent
13427f7
commit 62e6c27
Showing
6 changed files
with
165 additions
and
39 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
...p/(public)/(content)/react/utils/use-render/demos/class-name/css-modules/index.module.css
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,38 @@ | ||
.Button { | ||
box-sizing: border-box; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
height: 2.5rem; | ||
padding: 0 0.875rem; | ||
margin: 0; | ||
outline: 0; | ||
border: 1px solid var(--color-gray-200); | ||
border-radius: 0.375rem; | ||
background-color: var(--color-gray-50); | ||
font-family: inherit; | ||
font-size: 1rem; | ||
font-weight: 500; | ||
line-height: 1.5rem; | ||
color: var(--color-gray-900); | ||
user-select: none; | ||
|
||
@media (hover: hover) { | ||
&:hover { | ||
background-color: var(--color-gray-100); | ||
} | ||
} | ||
|
||
&:active { | ||
background-color: var(--color-gray-100); | ||
} | ||
|
||
&:focus-visible { | ||
outline: 2px solid var(--color-blue); | ||
outline-offset: -1px; | ||
} | ||
} | ||
|
||
.odd { | ||
color: var(--color-red); | ||
} |
46 changes: 46 additions & 0 deletions
46
.../src/app/(public)/(content)/react/utils/use-render/demos/class-name/css-modules/index.tsx
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,46 @@ | ||
/* eslint-disable react/button-has-type, jsx-a11y/control-has-associated-label */ | ||
'use client'; | ||
import * as React from 'react'; | ||
import { useRender } from '@base-ui-components/react/use-render'; | ||
import styles from './index.module.css'; | ||
|
||
type CounterState = { | ||
odd: boolean; | ||
}; | ||
|
||
type CounterProps = { | ||
className?: string | ((state: CounterState) => string); | ||
render?: useRender.RenderProp<CounterState>; | ||
}; | ||
|
||
function Counter(props: CounterProps) { | ||
const { render, className, ...otherProps } = props; | ||
const [count, setCount] = React.useState(0); | ||
const odd = count % 2 === 1; | ||
const state = React.useMemo(() => ({ odd }), [odd]); | ||
|
||
const { renderElement } = useRender({ | ||
render: render ?? <button />, | ||
state, | ||
className, | ||
props: { | ||
...otherProps, | ||
type: 'button', | ||
children: `Counter: ${count}`, | ||
onClick: () => setCount((prev) => prev + 1), | ||
'aria-label': `Count is ${count}, click to increase`, | ||
}, | ||
}); | ||
|
||
return renderElement(); | ||
} | ||
|
||
export default function ExampleCounter() { | ||
return ( | ||
<Counter | ||
className={(state) => | ||
state.odd ? `${styles.Button} ${styles.odd}` : styles.Button | ||
} | ||
/> | ||
); | ||
} |
2 changes: 2 additions & 0 deletions
2
docs/src/app/(public)/(content)/react/utils/use-render/demos/class-name/index.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,2 @@ | ||
'use client'; | ||
export { default as CssModules } from './css-modules'; |
39 changes: 32 additions & 7 deletions
39
...blic)/(content)/react/utils/use-render/demos/data-attributes/css-modules/index.module.css
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 |
---|---|---|
@@ -1,13 +1,38 @@ | ||
.Text { | ||
font-size: 0.875rem; | ||
line-height: 1rem; | ||
.Button { | ||
box-sizing: border-box; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
height: 2.5rem; | ||
padding: 0 0.875rem; | ||
margin: 0; | ||
outline: 0; | ||
border: 1px solid var(--color-gray-200); | ||
border-radius: 0.375rem; | ||
background-color: var(--color-gray-50); | ||
font-family: inherit; | ||
font-size: 1rem; | ||
font-weight: 500; | ||
line-height: 1.5rem; | ||
color: var(--color-gray-900); | ||
user-select: none; | ||
|
||
&[data-size='small'] { | ||
font-size: 0.75rem; | ||
@media (hover: hover) { | ||
&:hover { | ||
background-color: var(--color-gray-100); | ||
} | ||
} | ||
|
||
&[data-size='large'] { | ||
font-size: 1.25rem; | ||
&:active { | ||
background-color: var(--color-gray-100); | ||
} | ||
|
||
&:focus-visible { | ||
outline: 2px solid var(--color-blue); | ||
outline-offset: -1px; | ||
} | ||
|
||
&[data-odd] { | ||
color: var(--color-red); | ||
} | ||
} |
46 changes: 22 additions & 24 deletions
46
...app/(public)/(content)/react/utils/use-render/demos/data-attributes/css-modules/index.tsx
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 |
---|---|---|
@@ -1,42 +1,40 @@ | ||
/* eslint-disable react/button-has-type, jsx-a11y/control-has-associated-label */ | ||
'use client'; | ||
import * as React from 'react'; | ||
import { useRender } from '@base-ui-components/react/use-render'; | ||
import styles from './index.module.css'; | ||
|
||
type Size = 'small' | 'medium' | 'large'; | ||
|
||
type TextState = { | ||
size: Size; | ||
type CounterState = { | ||
odd: boolean; | ||
}; | ||
|
||
type TextProps = { | ||
className?: string | ((state: TextState) => string); | ||
render?: useRender.RenderProp<TextState>; | ||
size?: Size; | ||
children: React.ReactNode; | ||
type CounterProps = { | ||
className?: string; | ||
render?: useRender.RenderProp<CounterState>; | ||
}; | ||
|
||
function Text(props: TextProps) { | ||
const { className, render, size = 'medium', ...otherProps } = props; | ||
|
||
const state = React.useMemo(() => ({ size }), [size]); | ||
function Counter(props: CounterProps) { | ||
const { render, className, ...otherProps } = props; | ||
const [count, setCount] = React.useState(0); | ||
const odd = count % 2 === 1; | ||
const state = React.useMemo(() => ({ odd }), [odd]); | ||
|
||
const { renderElement } = useRender({ | ||
render: render ?? <p />, | ||
state, | ||
render: render ?? <button />, | ||
className, | ||
props: { ...otherProps, className: styles.Text }, | ||
state, | ||
props: { | ||
...otherProps, | ||
type: 'button', | ||
children: `Counter: ${count}`, | ||
onClick: () => setCount((prev) => prev + 1), | ||
'aria-label': `Count is ${count}, click to increase`, | ||
}, | ||
}); | ||
|
||
return renderElement(); | ||
} | ||
|
||
export default function ExampleText() { | ||
return ( | ||
<div> | ||
<Text size="small">Small size</Text> | ||
<Text size="medium">Medium size</Text> | ||
<Text size="large">Large size</Text> | ||
</div> | ||
); | ||
export default function ExampleCounter() { | ||
return <Counter className={styles.Button} />; | ||
} |
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