Skip to content
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

Allow boxClassName to be a function #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

matthieutirelli-pro
Copy link

@matthieutirelli-pro matthieutirelli-pro commented Mar 10, 2022

Hello !
Thanks for your project that we really appreciate.
We faced an issue on our project, we want to have a grid with items taking specific classes ( on our case, to hide them without destroying the children from our list )
I changed the code for the boxClassName to allow a function that use the children as argument to determine the classes to apply to the Grid Item.

On our case we use it that way for instance :

 const getBoxClassName = (children) => {
    const { streamId } = children.props;
    return clsx({
      ["hide"]: lastTalkerStreamId !== streamId,
    });
  };


      <PackedGrid
        className={clsx("packedGrid", classes.packedGrid)}
        boxAspectRatio={16 / 9}
        updateLayoutRef={packedGridRef}
        boxClassName={getBoxClassName}
      >

@matthieutirelli-pro
Copy link
Author

I'm not very used to Typescript, so i'm not really sure for the syntax of the interface, i hope this could contribute to your project :)

Copy link
Owner

@mxmul mxmul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! There's one small nit I'd like addressed, but the problem makes sense and it's something I'd like to see solved.

@@ -1,10 +1,13 @@
import React, { useState, useEffect, useRef, useCallback } from 'react'
import { largestRect } from 'rect-scaler'

type FunctionBoxClassNameType = (child: React.ReactNode) => string;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we avoid passing around and introspecting React nodes like this? The pattern is discouraged / deprecated

Maybe instead, we only pass the index of the child?

interface Props {
children: React.ReactNode
className?: string
boxClassName?: string
boxClassName?: string | FunctionBoxClassNameType
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could just inline the FunctionBoxClassNameType here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants