react-styled-flexboxgrid different config for different viewport - reactjs

I'm using this grid library in project https://github.com/yldio/joyent-react-styled-flexboxgrid and I would like to add 3 different configs for 3 different screen sizes (different gutterWidth and columns count).
In doc I see only possibility to create one config and pass it through theme.
Is anybody here who knows any solution or any other library that meets my requirement.
Edit: Project uses NextJS for SSR.

It looks to me that you'd use the ThemeProvider and change the theme prop based on the screen width.
import React from 'react'
import {ThemeProvider} from 'styled-components'
import {Grid, Col, Row} from 'react-styled-flexboxgrid'
const theme = {
flexboxgrid: {
// Defaults
gridSize: 12, // columns
gutterWidth: 1, // rem
outerMargin: 2, // rem
mediaQuery: 'only screen',
container: {
sm: 46, // rem
md: 61, // rem
lg: 76 // rem
},
breakpoints: {
xs: 0, // em
sm: 48, // em
md: 64, // em
lg: 75 // em
}
}
}
const sizedTheme =
(typeof window !== 'undefined') &&
(window.innerWidth < 500) ? { ...theme, gutterWidth: .5 } : theme;
const App = props =>
<ThemeProvider theme={sizedTheme}>
<Grid>
<Row>
<Col xs={6} md={3}>Hello, world!</Col>
</Row>
</Grid>
</ThemeProvider>
Something like this, adjusted to your goals.

Related

Interpolated opacity is not initially invisible

I am trying to create an interpolation on opacity. I create the clock, and decide the start time and end time of the animation. I interpolate it so it starts at 0, however my view is visible. Do you know why it's visible? Is it the clock is created not at default of 0 but of current time? Is it possible to initialize my animStartTime at the same value as clock?
Here is an expo showing the problem and code and screenshot below. We see a black square of 48x48. It should be invisible but its at full 100% opacity.
https://snack.expo.io/#noitidart/reanimated-interpolation-not-initially-invisible
import React from 'react';
import { View, StyleSheet } from 'react-native';
import Reanimated from 'react-native-reanimated';
export default function App() {
const animClock = new Reanimated.Clock();
// determine the times it will run from
const duration = 1400;
const animStartTime = new Reanimated.Value(0);
const animEndTime = Reanimated.add(animStartTime, duration);
// create the opacity
const animFrom = new Reanimated.Value(0);
const animTo = new Reanimated.Value(1);
const opacity = Reanimated.interpolate(animClock, {
inputRange: [animStartTime, animEndTime],
outputRange: [animFrom, animTo],
extrapolate: Reanimated.Extrapolate.CLAMP,
});
return (
<View style={{ justifyContent: 'center', alignItems: 'center', flex: 1 }}>
<Reanimated.View
style={{ width: 48, height: 48, backgroundColor: 'black', opacity }}
/>
</View>
);
}
I modified some part of your code and you can see it there

MaterialUi custom breakpoints not applied

Based on this question, I tried to have a theme that uses a common config file(that should be used by other themes as well). So I figured the breakpoints would be a good starting point since they should be the same across all the themes.
I have the common part like
const BREAKPOINTS = {
xs: 0,
sm: 768,
md: 960,
lg: 1280,
xl: 1920,
};
const commonConstants = {
breakpoints: {
values: BREAKPOINTS,
},
};
export default commonConstants;
Then I create my theme like
const defaultTheme = responsiveFontSizes(createMuiTheme(myTheme, commonConstants));
If I console.log my theme object, it will display the correct breakpoint values but will not apply them(the default one will be applied). However, if the breakpoint object is added directly inside the theme objectmyTheme (i.e. not uside of the theme), the correct breakpoints are applied. What am I missing here? If the final theme object has the same structure, why is it working differently?
Several portions of the theme (e.g. palette, breakpoints, spacing, typography) have additional processing associated with them that creates additional entries in the theme based on the options passed in. This additional processing is only applied to the object passed as the first argument to createMuiTheme. Any additional arguments are merged in after the additional processing.
For breakpoints, that additional processing is contained in the createBreakpoints function. This creates the various functions that leverage the breakpoint values (e.g. theme.breakpoints.up, theme.breakpoints.down, etc.). When you pass in custom breakpoint values via a second argument, those values are not being used in the creation of those breakpoint functions.
There are two main options for addressing this.
Option 1: Apply the additional processing yourself
import { createMuiTheme } from "#material-ui/core/styles";
import createBreakpoints from "#material-ui/core/styles/createBreakpoints";
const BREAKPOINTS = {
xs: 0,
sm: 768,
md: 960,
lg: 1280,
xl: 1920
};
const breakpointsFull = {
breakpoints: createBreakpoints({
values: BREAKPOINTS
})
};
const myTheme = { other: "stuff" };
const theme = createMuiTheme(myTheme, breakpointsFull);
Option 2: Merge your custom breakpoint values into the first argument to createMuiTheme
import { createMuiTheme } from "#material-ui/core/styles";
const BREAKPOINTS = {
xs: 0,
sm: 768,
md: 960,
lg: 1280,
xl: 1920
};
const breakpointsValues = {
breakpoints: {
values: BREAKPOINTS
}
};
const myTheme = { other: "stuff" };
const theme = createMuiTheme({ ...myTheme, ...breakpointsValues });
Here's a working example demonstrating the problem with your current approach as well as the two alternatives:
import React from "react";
import { createMuiTheme } from "#material-ui/core/styles";
import createBreakpoints from "#material-ui/core/styles/createBreakpoints";
const BREAKPOINTS = {
xs: 0,
sm: 768,
md: 960,
lg: 1280,
xl: 1920
};
const breakpointsValues = {
breakpoints: {
values: BREAKPOINTS
}
};
const breakpointsFull = {
breakpoints: createBreakpoints({
values: BREAKPOINTS
})
};
const myTheme = { other: "stuff" };
const badTheme = createMuiTheme(myTheme, breakpointsValues);
const goodTheme1 = createMuiTheme(myTheme, breakpointsFull);
const goodTheme2 = createMuiTheme({ ...myTheme, ...breakpointsValues });
export default function App() {
return (
<ul>
<li>badTheme.breakpoints.values.sm: {badTheme.breakpoints.values.sm}</li>
<li>badTheme.breakpoints.up("sm"): {badTheme.breakpoints.up("sm")}</li>
<li>
goodTheme1.breakpoints.values.sm: {goodTheme1.breakpoints.values.sm}
</li>
<li>
goodTheme1.breakpoints.up("sm"): {goodTheme1.breakpoints.up("sm")}
</li>
<li>
goodTheme2.breakpoints.values.sm: {goodTheme2.breakpoints.values.sm}
</li>
<li>
goodTheme2.breakpoints.up("sm"): {goodTheme2.breakpoints.up("sm")}
</li>
</ul>
);
}
In order to effectively override breakpoints in the default MuiTheme (as shown in Ryan Cogswell answer Option 1) you must use the createBreakpoints function passing in the values and keys elements.
Based on Ryan's answer, I found the following solution to work.
import { createMuiTheme } from "#material-ui/core/styles";
import createBreakpoints from "#material-ui/core/styles/createBreakpoints";
const BREAKPOINTS = {
xs: 0,
sm: 768,
md: 960,
lg: 1280,
xl: 1920
};
const breakpointsFull = createBreakpoints({
values: {...BREAKPOINTS},
keys: Object.keys(BREAKPOINTS),
});
const myTheme = { other: "stuff" };
const theme = createMuiTheme({
default: myTheme,
breakpoints: breakpointsFull,
});
This strategy enables easy modification of the BREAKPOINTS variable, and correctly assigns it into the object. Using the createBreakpoints Mui function will generate functions for up and down, which allow you to check against breakpoints in your code for example:
const isSmallScreen = UseMediaQuery(theme.breakpoints.down('sm'))

Can somone help me understand why these arrows arent showing IN react-multi-carousel?

Been researching for days, the arrows aren't showing on react-carousel
the picture looks like this: Image Of No arrows
Im using Next .js
It is my first time I havent had this problem with regular react apps
import './carousel.scss'
import Carousel from 'react-multi-carousel';
import { ProductContext } from '../../pages/oniContext';
import { CardComp } from '../cards/card';
import {Button} from '../../components/common/button';
import {customArrows} from './customArrows'
import 'react-multi-carousel/lib/styles.css';
import React, {useState,useEffect,useContext} from 'react'
const responsive = {
superLargeDesktop: {
// the naming can be any, depends on you.
breakpoint: { max: 4000, min: 3000 },
items: 5,
},
desktop: {
breakpoint: { max: 3000, min: 1024 },
items: 1,
},
tablet: {
breakpoint: { max: 1024, min: 464 },
items: 2,
},
mobile: {
breakpoint: { max: 464, min: 0 },
items: 1,
},
};
export const PackageCarousel = (props) => {
const productConsumer = useContext(ProductContext);
return (
<Carousel
swipeable={true}
responsive={responsive}
>
{productConsumer.activePackage.map((service, index) => (
<CardComp
key={index}
title={service.title}
text={service.content}
addOns={service.addOns}
image={service.src}
/>
))}
</Carousel>
);
}
Just add the below line in your head section of the webpage:
<link href="//db.onlinewebfonts.com/c/0e979bd4a3c1c6ac788ed57ac569667f?family=revicons" rel="stylesheet" type="text/css"/>
I was facing same issue and after adding above line 'revicons' font loads properly.
Digging through the source code here => https://github.com/YIZHUANG/react-multi-carousel/blob/master/src/assets/styles.css
It appears the arrow icons are coming from custom font files that are in eot woff formats.
This type of an outcome (boxes instead of icons ) is an indication the font is not properly downloaded and doesn't have the right mime type probably. See what the browser is downloading for revicons ( the font being used by the library) - If its incorrect or not downloaded, you may see the issue.
You have 3 options if you can confirm the fonts are indeed the issue
1.You may need to specify a loader https://www.npmjs.com/package/next-fonts to rectify the issue and get the fonts in.
You maybe able to also load the font in the head and let the reference work out itself.
You can modify the module to reference the fonts or inline them
If you can post your code in a online sandbox, I can assist more!
Best of luck!

How to set breakpoints when overriding theme variables in createMuiTheme

I'm trying to set the default theme typography font sizes to change based on breakpoints. For example, when the breakpoint is at the {xs} value, change theme.typography.h4.fontSize to '1.5rem'. At all other breakpoints, leave it at the default value ('2.125rem').
I can do this easily in components using the following code example:
const useStyles = makeStyles(theme => ({
title: {
[theme.breakpoints.down('xs')]: {
fontSize: '1.5rem',
}
},
}))
However, this means having to add the same code to every single component that has <Typography variant='h4'> in it. If I decide to change the value from '1.5rem' to '1.25rem', I would need to locate every single instance of <Typography variant='h4'> and change it manually.
Is there any way to add breakpoints to createMuiTheme so it applies to all instances?
I have attempted the following, which does not work:
const defaultTheme = createMuiTheme()
const theme = createMuiTheme({
typography: {
h4: {
[defaultTheme.breakpoints.down('xs')]: {
fontSize: '1.5rem'
}
}
}
})
You can use custom breakpoints with createBreakpoints.
Here is an example changing the MuiButton
import createBreakpoints from '#material-ui/core/styles/createBreakpoints'
const customBreakpointValues = {
values: {
xs: 0,
sm: 576,
md: 768,
lg: 992,
xl: 1200,
},
}
const breakpoints = createBreakpoints({ ...customBreakpointValues })
const theme = createMuiTheme({
breakpoints: {
...customBreakpointValues,
},
components: {
MuiButton: {
styleOverrides: {
root: {
padding: '10px',
[breakpoints.up('lg')]: {
padding: '20px',
},
},
},
},
},
})
CORRECTION: Initially I indicated that your solution didn't work because you were using an impossible condition, but I was incorrect. "down" from xs is inclusive of xs, so it does what you would want (matches 0px to 600px).
I'm not sure what I did in my initial testing that led me astray, but one thing that can cause confusion is if you have multiple down thresholds. In my own testing, I had an easier time avoiding shooting myself in the foot (e.g. by having a breakpoints.down("xs") followed by a later breakpoints.down("sm") that trumped the "xs" settings) by using breakpoints.only.
From https://material-ui.com/layout/breakpoints/#breakpoints
xs, extra-small: 0px or larger
sm, small: 600px or larger
Here is an updated sandbox that shows a little more clearly what is happening:

material-ui: AppBar: strategy for restricting an image height to AppBar height?

can anyone provide guidance around an idiomatic way to place an image in an AppBar and have it be restricted to the standard material height (e.g. 64px for a desktop)?
i'm currently using material-ui#next (1.0.0-beta.2 currently).
i have found that something like:
<AppBar>
<Toolbar>
<IconButton color="contrast" aria-label="Menu">
<MenuIcon />
</IconButton>
<img src={logo} style={{height: 64}}/>
<Typography type="title" color="inherit">
React Scratch
</Typography>
</Toolbar>
</AppBar>
works well.
the actual logo is a png file with a height greater than 64, so if i don't ratchet it down, it expands the height of the AppBar out of Material spec.
in the current master branch version of src/styles there is a getMuiTheme.js which seems to deliver this height readily, but in the #next version i am looking at, that file doesn't even exist and tbh, i can't easily determine how that height is being set anymore.
i found that the AppBar is currently being renovated for composability, so that churn might make it challenging to answer this question, but just in case anyone has a good handle on this, i figured i would toss the question out there.
thanks!
In all cases I've seen, an AppBar is implemented with a Toolbar as it's first child. The Toolbar's stylesheet dictates it's height based on the breakpoints defined in the theme.
Take a look here: https://github.com/callemall/material-ui/blob/v1-beta/src/Toolbar/Toolbar.js
You can use a similar approach to define a stylesheet with a class for your AppBar images that varies the height for the applicable breakpoints. Then when rendering the component, apply the class to your image.
Note: if you use the withStyles HOC, as is done in the Toolbar, AppBar etc, the classes defined in that stylesheet will be available through a prop named classes.
You are right about the AppBar's need for composability, but that issue has not been solved yet, and this is the beta branch anyway. When it is solved, there should be a better solution that would be worth migrating towards.
I hope this answer helps. I would have added code samples but I am answering from my phone while waiting in a grocery store parking lot. If I get a chance I will update this answer.
Here's one approach, duplicating the styles in a new reusable component:
import createStyleSheet from 'material-ui/styles/createStyleSheet';
import withStyles from 'material-ui/styles/withStyles';
// define these styles once, if changes are needed because of a change
// to the material-ui beta branch, the impact is minimal
const styleSheet = createStyleSheet('ToolbarImage', theme => ({
root: {
height: 56,
[`${theme.breakpoints.up('xs')} and (orientation: landscape)`]: {
height: 48,
},
[theme.breakpoints.up('sm')]: {
height: 64,
},
},
}));
// a reusable component for any image you'd need in a toolbar/appbar
const ToolbarImage = (props) => {
const { src, classes } = this.props;
return (
<img src={src} className={classes.root} />
);
};
// this higher order component uses styleSheet to add
// a classes prop that contains the name of the classes
export default withStyles(styleSheet)(ToolbarImage);
Another approach is to add the standard toolbar heights to the theme as business variables, override the root class for all Toolbars so that it makes use of them, and use the theme whenever you need to reference them again:
// define the standard heights in one place
const toolbarHeights = {
mobilePortrait: 56,
mobileLandscape: 48,
tabletDesktop: 64,
};
// create the theme as you normally would, but add the heights
let theme = createMuiTheme({
palette: createPalette({
primary: blue,
accent: pink,
}),
standards: {
toolbar: {
heights: toolbarHeights,
},
},
});
// recreate the theme, overriding the toolbar's root class
theme = createMuiTheme({
...theme,
overrides: {
MuiToolbar: {
// Name of the styleSheet
root: {
position: 'relative',
display: 'flex',
alignItems: 'center',
minHeight: theme.standards.toolbar.heights.mobilePortrait,
[`${theme.breakpoints.up('xs')} and (orientation: landscape)`]: {
minHeight: theme.standards.toolbar.heights.mobileLandscape,
},
[theme.breakpoints.up('sm')]: {
minHeight: theme.standards.toolbar.heights.tabletDesktop,
},
},
},
},
});
Then you can reference these heights in any stylesheet you create because they're part of the theme.
UPDATED FOLLOWING THE RELEASE OF 1.0.0-beta.11:
There is now a toolbar mixin available on the theme that provides the toolbar minHeight for each breakpoint. If you need to style an element relative to the standard height of the AppBar component, you can use this object to build your own styles:
const toolbarRelativeProperties = (property, modifier = value => value) => theme =>
Object.keys(theme.mixins.toolbar).reduce((style, key) => {
const value = theme.mixins.toolbar[key];
if (key === 'minHeight') {
return { ...style, [property]: modifier(value) };
}
if (value.minHeight !== undefined) {
return { ...style, [key]: { [property]: modifier(value.minHeight) } };
}
return style;
}, {});
In this example, toolbarRelativeProperties returns a function that will return an object that can be spread into your style object. It addresses the simple case of setting a specified property to a value that is based on the AppBar height.
A simple usage example would be the generation of a dynamic CSS expression for height calculation, which is depending on the standard height of the AppBar:
const componentStyle = theme => ({
root: {
height: toolbarRelativeProperties('height', value => `calc(100% - ${value}px)`)(theme)
}
});
The generated style definition might look like this:
{
height: 'calc(100% - 56px)',
'#media (min-width:0px) and (orientation: landscape)': {
height: 'calc(100% - 48px)'
},
'#media (min-width:600px)': {
height: 'calc(100% - 64px)'
}
}

Resources