I am trying to generate a bunch of styles through an each loop in SASS. The styles should only be generated, if the variable the each loop is looking at right now is set.
I tried different variable "styles", e.g.: $use-#{$type}, but I'm kinda lost. Even tried to do it with a function, but it seems like functions can not access the variables of loops.
$typo: (t1, t2);
$use-t1: 1; $t1-color: black;
$use-t2: 1; $t2-color: black;
#each $type in $typo{
#if $#{use-$type} == 1{
.#{$type}{
color: $#{$type}-color;
}
}
}
I would expect the variables in the first round of the each loop to be:
$#{use-$type} -> $use-t1 -> 1
$#{$type}-color -> $t1-color -> black
But both throw "Expected identifier." or "Unknown variable", depending on how I try it.
You can't reference a variable using interpolation – in your case $#{$type}-color.
I would recommend you to use a map instead – like:
$map: (
t1: (use: 1, color: black),
t2: (use: 2, color: white)
);
#each $key, $value in $map {
#if map-get($value, use) == 1 {
.#{$key} { color: map-get($value, color); }
}
}
// output
.t1 { color: black; }
As a side note it's worth knowing Sass will not print properties with null values or classes without properties – why you can do the above without checking use
$map: (
t1: (color: black),
t2: (color: null) // no color => no prop => empty class => nothing printed
);
#each $key, $value in $map {
.#{$key} { color: map-get($value, color); }
}
// output
.t1 { color: black; }
Related
IntroJs hints
How can I skip or hide when a parent element is not visible? For some reason only inline seems to be working for my hints.I have the data-hints in spans on the HTML and I need to check if the nearest element is visible or parent/child element.
var hints = false;
var all = document.getElementsByTagName("*");
function introFunction() {
for (var i = 0, max = all.length; i < max; i++) {
if ((isHidden(all[i]) && hints));
document.getElementById("#helpFunc").html("Show Help");
introJs().hideHints();
} else {
document.getElementById("#helpFunc").html("Hide Help");
introJs().showHints();
}
hints = !hints;
function isHidden(el) {
var style = window.getComputedStyle(el);
return ((style.display === 'none') || (style.visibility === 'hidden'));
}
}
Instead of trying to get the computed style of the element, try checking the element bounding rectangles - if there aren't any, it means the element is not visible.
!!el.getClientRects().length; // false means el or its parent(s) is hidden
LE: that doesn't work for "visibility: hidden;" though. So maybe you can combine the above with what you already have.
I want to show a list in flutter and I'm using listView. The thing is I just want to show 5 items, by this I mean that When the user scrolls down I want to remove from the beginning index and add another widget to the end of the list that contains my widgets, but when I do that the ScrollView instead of staying where it is(for example showing the item in the index 3) it goes to the next item(it jumps where the item 4 is).
My data is kinda expensive and I can't keep them I have to remove them from the beginning. I'm really stuck I would really appreciate some help
To limit an Iterable(List is an Iterable) to n elements, you can use the .take(int count) method.
// A list of 0 - 999 ( 1,000 elements)
var nums = [for(int i = 0; i < 1000; i++) i];
// Take only the first 5, then print
print(nums.take(5));
The Iterable returned is lazy. It doesn't trim the list or anything. It's just an iterator that will only produce at most count values
Additionally, you can use the .skip(int count) method to skip the first count values in the list.
Combine the 2 for something like this:
// skips 0-4, takes 5,6,7,8,9
print(nums.skip(5).take(5));
I am not 100% sure what your problem is. If you want to build widgets as you go, you can use ListView.builder widget. Give it an itemBuilder and an optional itemCount. It will build as you go and delete the unseen widgets.
ListView.builder(
itemCount: myList.length,
itemBuilder: (context, index) => Card(
child: ListTile(
title: Text(myList[index]),
),
),
),
),
Check out this doc
itemCount: 5, <<this will max your list
if your list less than 5 it will have error, so simply used this:
int limit_transaction = 0;
then in your FutureBuilder:
builder: (context, snapshot) {
if (snapshot.hasData) {
List<LastTransaction> list = snapshot.data;
if(list.length == 1){
limit_transaction = 1;
} else if(list.length == 2){
limit_transaction = 2;
} else if(list.length == 3){
limit_transaction = 3;
} else if(list.length == 4){
limit_transaction = 4;
} else if(list.length == 5){
limit_transaction = 5;
}
Last Step in Listview, just parse this:
itemCount: limit_transaction,
I have left/right arrows on a page and I want to pick the animation without having to define a relationship between all the routes. Is it possible to set it on the {{link-to}}? Right now it's pretty brittle.
I have been looking, and I don't think it's possible to know what link was clicked from the transition it caused. However, I can think of two different ways to tackle your use case.
Solution 1: metaprogramming
Make a list of your routes and generate transitions dynamically from it. Something like this:
// app/transitions.js
export default function() {
const orderedRoutes = [
'left-route',
'center-route',
'right-route',
];
// See https://github.com/coleww/each-cons
// where I pinched this from
function eachCons(a, n) {
var r = []
for (var i = 0; i < a.length - n + 1; i++) {
r.push(range(a, i, n))
}
return r
}
function range (a, i, n) {
var r = []
for (var j = 0; j < n; j++) {
r.push(a[i + j])
}
return r
}
eachCons(orderedRoutes, 2).forEach(pair => {
// `pair` will be each pair of consecutive routes
// on our `orderedRoutes` list
const left = pair[0];
const right = pair[1];
// For each pair, define a transition
this.transition(
this.fromRoute(left),
this.toRoute(right),
this.use('toLeft'),
this.reverse('toRight')
);
});
}
Note that I only define transitions for adjacent routes. If you want to define a transition between left-route and center-route, you'll need to alter the algorithm to define new combinations.
Solution 2: callback to fromRoute
The function fromRoute can not only take a string, but also a function. This function receives two parameters: the names of the initial and the final routes of a transition. In this function you can return true if the transition should apply, and false otherwise. See here:
http://ember-animation.github.io/liquid-fire/#/transition-map/route-constraints
You can use this function to decide whether you should be going left or right (as per your use case). See this:
// app/transitions.js
export default function() {
// Names of the routes in the left-right succession
const orderedRoutes = [
'left-route',
'center-route',
'right-route',
];
function isLeft(initial, destination) {
const i0 = orderedRoutes.indexOf(initial);
const i1 = orderedRoutes.indexOf(destination);
if (i0 === -1 || i1 === -1) {
// This is not one of the transitions
// in the left-right succession
return false;
}
if (i0 === i1) {
// They are the same route
return false;
}
// This will be `true` if the initial route
// is "to the left" of the destination route
return i0 < i1;
}
this.transition(
this.fromRoute(isLeft),
this.use('toLeft')
this.reverse('toRight')
);
}
In this example, for each transition we check the initial and the destination route. We see if they belong to the left-right succession, and whether the transition corresponds to a "left" or a "right". If it's a "left" we return true in the "toLeft" case. If it's a "right", we return true in the "toRight" case.
Am newbie to ReactJS. I want to use a IF block inside the render function. While search for this I got result like "You should use ternary operator instead of IF statement". But if I want to use something like,
$.each(array, function(i, x) {
var flag = 0
if({x.a1} || !{x.a2}) {
flag = 1;
<p>true flag enabled</p>
}
<p>...</p>
});
How to convert this statement into JSX syntax or how to use it in React render fucntion.
Thanks in advance.
This link will help you
https://facebook.github.io/react/tips/if-else-in-JSX.html
But I'd use something like this, as its slightly easier to read (IMHO). Note, your array is a prop - passed into the component (or could be a state). I'd use lodash for mapping etc, cause its so useful all over the place (https://lodash.com/)
_renderElements: function(){
return _.map(this.props.array, function(el){
var flag = 0;
return el.a1 || el.a2 ? <p>{'true 1 enabled'}</p> : <p>...</p>;
})
},
render: function () {
return (
{this._renderElements()}
}
);
}
Hope that's helpful.
I do this in one of two ways, depending mostly on how big the if statement is.
one scenario, I don't know if I'm going to render an element or not:
Component = React.createClass({
render() {
var elem;
if (something) {
elem = (<SomeOtherComponent />);
}
return (
<div>{elem}</div>
);
}
});
This is basically a way to either show the element/component or not. If I'm going to map something I would use a separate method and call it:
Component = React.createClass({
mapIt() {
return this.props.items.map(item => {
... do your stuff ...
return (
<SomeOtherComponent prop1={item.value} ... />
);
});
},
render() {
return (
{this.mapIt()}
);
}
});
This to me is a nice clean way of handling.
You want to have your render function look something like this:
render: function () {
return (
{
array.map(function (el, i) {
var flag = 0;
if (el.a1 || el.a2) {
flag = 1;
return <p>true flag enabled</p>;
} else {
return <p>...</p>;
}
}
}
);
}
React allows you to return an array of React elements, so you can map your array and return a JSX element for every element of the array.
How to add additional style if the value is passed? Or is there any way to do it?
#mixin link($color, $color-hover:false){
color: $color;
#if $color-hover !== false {
&:hover { color: $color-hover; }
}
}
Is != not working for you? I suppose you are trying with !==. i.e. a Double '=' instead of a single '=' prefixed with '!'. For SCSS, equality operators (==, !=) are supported for all types.