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

Responsive - deactivate stickybox on mobile devices #46

Open
adrienlamotte opened this issue Jan 31, 2020 · 8 comments
Open

Responsive - deactivate stickybox on mobile devices #46

adrienlamotte opened this issue Jan 31, 2020 · 8 comments

Comments

@adrienlamotte
Copy link

Hello,

Most of the time when using a sticky sidebar, you want the sidebar not to be sticky on small devices.
Is there any way to achieve this ?

Thank you for your work, great lib btw :)

@johnsonthedev
Copy link

did you find a solution to it ? I have same challenge

@adrienlamotte
Copy link
Author

@johnson444 well for now I define a conditionnal component like :

const StickyWrapper = isLargeScreen ? StickyBox : React.Fragment;

Where isLargeScreen is your test to check the breakpoint.

@johnsonthedev
Copy link

@adrienlamotte Thx ! Will do this as well. If I have time I will look closer into the package. If I find a better way I will let you know

@endze1t
Copy link

endze1t commented Sep 20, 2020

@johnson444 well for now I define a conditionnal component like :

const StickyWrapper = isLargeScreen ? StickyBox : React.Fragment;

Where isLargeScreen is your test to check the breakpoint.

This solution might work, when the children can be re-rendered. In case that the children should not be re-rendered this might kill some flow.

I would prefer like a prop disable , that just disable the calculcation from StickyBox and act just an simple div

          <StickyBox
            offsetTop={160}
            offsetBottom={0}
            disable={!isLargeScreen}
          >

@Arnaud-Desportes
Copy link

Arnaud-Desportes commented Mar 28, 2021

A simple and fast solution to remove stickyBox on mobile is to add a css class and use @media to cancel the sticky effect according to the desired width. Here is an example :

// In component
<StickyBox className="stickyBox">
    <YourComponent />
</StickyBox>

/*CSS*/
@media (max-width: 1024px) { 
  .stickyBox{
    position: inherit !important;
    top: inherit !important;
  }
}

I hope it helps ++.

@simplenotezy
Copy link

@Arnaud-Desportes Nice solution, thanks for sharing!

@namipsg
Copy link

namipsg commented Jun 27, 2022

@Arnaud-Desportes

You saved me lots of hours, Thanks for the solution!

@antokhio
Copy link

antokhio commented Jun 6, 2024

MUI 5 solution thanks to @Arnaud-Desportes

import React from 'react';
import { SxProps, Theme, styled } from '@mui/material/styles';

import ReactStickyBox, { StickyBoxCompProps } from 'react-sticky-box';

export interface StickyBoxProps extends StickyBoxCompProps {
  disabled?: boolean;
  sx?: SxProps<Theme>;
}

const StickyBox = styled(ReactStickyBox, {
  shouldForwardProp: (prop) => ['disabled'].every((key) => key !== prop),
})<StickyBoxProps>(({ theme, disabled }) =>
  theme.unstable_sx({
    ...(disabled && {
      position: 'inherit !important',
      top: 'inherit !important',
    }),
  }),
);

export default StickyBox;

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

No branches or pull requests

7 participants