Add Gap Between Elements While Using React Virtualized - css

I am using React-Virtualized to create a lazy loading infinite list.
https://github.com/bvaughn/react-virtualized/blob/master/docs/InfiniteLoader.md
However, I am not able to create a gap between the rendered divs, since they are absolutely positioned and top is calculated dynamically.
I've tried the following, however no luck.
Any ideas on how to add this gap between each element? Thanks!
<AutoSizer disableHeight>
{({width}) => (
<List
onRowsRendered={onRowsRendered}
ref={registerChild}
rowCount={rowCount}
rowRenderer={rowRenderer}
width={width - 30}
rowHeight={175}
height={this.state.height - 64}
style={{
paddingTop: 15,
boxSizing: 'content-box',
}}
containerStyle={{
position: 'relative',
overflow: 'visible',
}}
/>
)}
</AutoSizer>

I ended up solving for the padding by adding a div inside the CellMeasurer as a parent container to provide the padding.
The div is the absolute positioned container, whereas the Card is padded and shows the box shadow.
<CellMeasurer
cache={this.cache}
columnIndex={0}
key={key}
rowIndex={index}
parent={parent}
>
{({ measure }) => (
<div
className={s.listItem}
style={style}
onLoad={measure}
key={index}>
<Card>

Build a div using padding-bottom arround your rendered item.
Example:
<div style={paddingBottom:10}>
// Your item component goes here
</div>
I also asked on GitHub what's the prefered way. See the question here: https://github.com/bvaughn/react-virtualized/issues/1786

You can just decrease the height, if one item is like 50 pixels and you want some spacing , do that to your cell renderer (rowRenderer prop):
style={{ ...style, height: CELL_HEIGHT - ROW_SPACING }}
So item will be placed on the exact same places based on the calculations of the library but they will be smaller and you will have space between them.
That would be your List component:
<List rowHeight={CELL_HEIGHT} .... />

Related

Center ScrollView in React Native

I've been trying to use React Native lately and I am trying to incorporate a ScrollView in my program. I am having issues centering the ScrollView in the middle of the page. I have looked at many of the posts here on SO and have been unable to get a working implementation. If someone could point me in the right direction, I would really appreciate it!
Screen:
<SafeAreaView style={styles.container}>
<ScrollView style={styles.scrollable}>
<ProfileGreeting />
<DailyGoals goals={GOALS} />
<CoursesContainer courses={COURSES} />
</ScrollView>
</SafeAreaView>
Styles:
const styles = {
container: {
height: "100%",
width: "100%",
display: "flex",
alignItems: "center",
flex: 1
},
scrollable: {
flexGrow : 1,
justifyContent : 'center'
}
}
As suggested in this this post I tried to use flexGrow on the ScrollView, but it still doesn't seem to work - it instead shifts to the side as such:
Try Scroll view "contentContainerStyle" propert instead of style
propert
ScrollView style defines the outer container of the ScrollView, e.g its height and relations to siblings elements
ScrollView contentContainerStyle defines the inner container of it, e.g items alignments, padding, etc

How do I use percentages with MotionValues in Framer Motion?

Using Framer Motion's useTransform I want to change the width of an element using a MotionValue that is a percent (eg. 75%) rather than in pixels.
The default assumes pixels:
<motion.div className="dynamic-element" style={{ width: w }}>
I want something like the following:
<motion.div className="dynamic-element" style={{ width:`${w}%` }}>
Which obviously doesn't work.
How can I specify that my MotionValue is a percentage and not pixel-based?
Alternatively, I can use filter: scale() but that results in the contents being blurry (and yes I've looked into how to fix that, but haven't had any success).
You can do it like this :
useTransform(w, (value) => `${value}%`);
Can you use variants?
E.g.:
<motion.div
variants={{
a: {width: '20%'},
b: {width: '50%'}
}}
animate={variant}
/>
See this CodeSandbox demo.
You can use percentages as the output value.
const width = useTransform(w, [0, someOtherNumberYouPick], ['0%', '100%'])
...
<motion.div className="dynamic-element" style={{ width }}>

Is there an easy way OR a workaround to display different looking image depending on the value of a variable in React?

I'm creating a website for my company. I'm quite inexperimented, I'm working there as an intern.
I have to do a reporting on this website. One of the fields in 5 hearts displaying depending the value of a variable: if the variable's value is 2.5, hearts 1 & 2 will be filled with red, heart 3 will be half red half gray and the others will be gray.
I did some researches about how to manipulate images in React and css, and I found about filters, and especially the grayscale option. Sadly, I couldnt find a way to set it only for a part of the image.
The idea would be to have a method to be called in my component's render method like this :
displayHearts = content => {
// content is a float, value between 0 and 5
// depending on the content, the return would be different
return (
<div>
<p>{content}</p>
--> display of 5 hearts, gray or red depending on content
</div>
);
}
Here is a sample of what I would like it to look like:
https://ibb.co/VYBzXtQ
It is possible for me to install any modules to help me do that.
Any help would be appreciated.
Thank you!
One way of going about it is to render 5 grey hearts and 5 absolutely positioned red hearts above it, and use the content prop to figure out how much of the red hearts that should be visible.
Example
function HeartScoreIndicator({ content }) {
return (
<div style={{ display: "inline-block", position: "relative" }}>
<div>
{Array.from({ length: 5 }, (_, index) => (
<img key={index} src="/images/grey-heart.png" />
))}
</div>
<div
style={{
position: "absolute",
top: 0,
left: 0,
width: `${content * 20}%`,
whiteSpace: "nowrap",
overflow: "hidden"
}}
>
{Array.from({ length: 5 }, (_, index) => (
<img key={index} src="/images/red-heart.png" />
))}
</div>
</div>
);
}

react-bootstrap-table - formatting - row height, text wrap

I am working with react-bootstrap-table and I am facing problems with formatting it. Main issues are:
the headers with long names should be presented with 2 lines of text, instead there is one and "..." like in the picture below:
Moreover in no way I could set the height of a single row of a table. Each text has big padding therefore the table is not too condensed:
And the code goes here:
<BootstrapTable
data={this.props.sales}
version="4"
striped
hover
pagination
keyField="Type"
>
{tableHeaders.map((header, index) => (
<TableHeaderColumn key={index} dataField={header.id} style={{ height: 10 }}>
{header.name}
</TableHeaderColumn>
))}
</BootstrapTable>
According to docs you can do all of the customizations you need.
First issue: To remove dots you can use thStyle property which you can pass to TableHeaderColumn component and override CSS white-space property.
<TableHeaderColumn thStyle={{ whiteSpace: 'normal' }} {...anotherProps} />
Second issue: You can handle height of your row using trClassName property. You can either pass string or function to handle each or make conditional class depends on row. See more here.
For example:
<BootstrapTable trClassName="customClass" {...anotherProps} />
And define your customClass:
.customClass {
// override padding or height whatever you want
padding: 3px;
}
Good luck and have fun!

react/semantic-ui: How to get a fullscreen segment

I would like to build a layout like the homepage example of semantic-ui: http://semantic-ui.com/examples/homepage.html
The first vertical black segment has nearly full height. It is done by the masthead class, but I don't understand from where this class comes.
I'm using react, so this is my layout so far:
render() {
return (
<div>
<Segment vertical inverted>
<p>Main</p>
</Segment>
<Segment vertical>
<p>First</p>
</Segment>
</div>
)
}
But with this the first segment doesn't have full height.
Take a look on this example with SUIR, source is also avialable. masthead class is defined in SUI example and isn't part of framework, these behaviour was implement with custom styling:
<Segment
inverted
textAlign='center'
style={{ minHeight: 700, padding: '1em 0em' }}
vertical
>

Resources