diff --git a/README.md b/README.md index 35264fe..345d79c 100644 --- a/README.md +++ b/README.md @@ -348,6 +348,8 @@ const ExampleAppMedia = createMedia({ interactions: { hover: "(hover: hover)", notHover: "(hover: none)", + landscape: "(orientation: landscape)", + portrait: "(orientation: portrait)", }, }) @@ -395,6 +397,28 @@ export const HomePage = () => { The examples given for each prop use breakpoint definitions as defined in the above ‘Setup’ section. +If you would like to avoid the underlying div that is generated by `` and +instead use your own element, use the render-props form: + +```tsx +export const HomePage = () => { + return ( + <> + Hello mobile! + + {(mediaClassNames) => { + return ( + + Hello desktop! + + ) + }} + + + ) +} +``` + #### createMediaStyle > Note: This is only used when SSR rendering diff --git a/src/Media.tsx b/src/Media.tsx index 1bf778b..7a123ae 100644 --- a/src/Media.tsx +++ b/src/Media.tsx @@ -11,10 +11,10 @@ import { intersection, propKey, createClassName } from "./Utils" * * @see {@link MediaProps.children}. */ -export type RenderProp = (( +export type RenderProp = ( className: string, renderChildren: boolean -) => React.ReactNode) +) => React.ReactNode // TODO: All of these props should be mutually exclusive. Using a union should // probably be made possible by https://github.com/Microsoft/TypeScript/pull/27408. @@ -176,6 +176,11 @@ export interface MediaProps extends MediaBreakpointProps { * */ children: React.ReactNode | RenderProp + + /** + * Additional classNames to passed down and applied to Media container + */ + className?: string } export interface MediaContextProviderProps { @@ -363,13 +368,22 @@ export function createMedia< validateProps(props) } + static defaultProps = { + className: "", + } + render() { const props = this.props return ( {({ onlyMatch } = {}) => { let className: string | null - const { children, interaction, ...breakpointProps } = props + const { + children, + className: passedClassName, + interaction, + ...breakpointProps + } = props if (props.interaction) { className = createClassName("interaction", props.interaction) } else { @@ -419,7 +433,7 @@ export function createMedia< } else { return (
{renderChildren ? props.children : null} diff --git a/src/__test__/Media.test.tsx b/src/__test__/Media.test.tsx index a7bd848..64997e6 100644 --- a/src/__test__/Media.test.tsx +++ b/src/__test__/Media.test.tsx @@ -134,6 +134,17 @@ describe("Media", () => { expect(query).toHaveStyleRule("margin", "0") expect(query).toHaveStyleRule("padding", "0") }) + + it("applies additional classNames passed as props", () => { + const query = renderer + .create( + + ohai + + ) + .toJSON() + expect(query.props.className).toContain("foo") + }) }) describe("concerning breakpoints", () => {