Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!--
inspired by https://dribbble.com/shots/1824088-GIF-for-the-Weather-App
using simpleweatherjs for results
-->
<section class="container">
  <div id="root"></div>
</section>
              
            
!

CSS

              
                @import url(https://fonts.googleapis.com/css?family=Lato:300,400);

/* Vars */
$body-background: #212121;
$body-color: #f5f5f5;

$animation-easing: cubic-bezier(1, -0.5, 0, 1);

$weather-background: #8ba892;
$weather-color: #fff;
$weather-title: #d1dcd3;
$border-color: #ce7d3b;

$pale-gold: #e3bb88;
$di_serria: #d89864;
$brandy-rose: #c1887d;
$santa-fe: #b1695a;
$rose-ebony: #644749;

$days: $pale-gold, $di-serria, $brandy-rose, $santa-fe, $rose-ebony;

@for $i from 1 through length($days) {
  .weather__item:nth-of-type(#{$i}) {
    background: nth($days, $i);
  }
}

/* Main Styles */
* {
  box-sizing: border-box;
}

html {
  font-size: 16px;
}

body {
  font-family: "Lato";
  background: $body-background;
  color: $body-color;
  line-height: 1.6;
  font-size: 1.4rem;
}

.container {
  margin: 20px auto;
  max-width: 320px;
}

/* Weather App*/
.weather {
  background: $weather-background;
  color: $weather-color;
  overflow: hidden;

  &__header {
    color: $weather-title;
    display: flex;
    font-size: 1rem;
    justify-content: space-between;
    padding: 20px;

    i {
      margin-right: 5px;
    }
  }
  
  &__list {
    list-style: none;
    margin: 0;
    padding: 0;
  }

  &__city,
  &__country {
  }

  &__item {
    border-bottom: 3px solid transparent;
    cursor: pointer;
    display: flex;
    flex-wrap: wrap;
    overflow: hidden;
    padding: 20px 10px;
    transition: border 0.3s 0s, padding 0.3s 0.5s;
    width: 100%;

    &:hover {
      border-color: $border-color;
    }

    .weather__icon,
    .weather__info {
      opacity: 0;
      transition: all .3s .3s $animation-easing;
    }

    .weather__info {
      transform: translateY(50px);
      transition: opacity 0.3s 0s $animation-easing,
        transform 0.3s 0s $animation-easing;
    }

    &.active {
      border-color: $border-color;
      padding: 50px 10px 150px;
      transition-delay: 0s;

      .weather__icon,
      .weather__info {
        opacity: 1;
        transform: translateX(0);
        transition-delay: 0.3s;
      }
    }
  }

  &__icon {
    width: 40%;
  }

  &__details {
    width: 60%;
  }

  &__icon {
    font-size: 3rem;
    position: relative;
    transform: translateX(-50px);

    i {
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
    }
  }

  &__details {
    padding-left: 30px;
    position: relative;
  }

  &__info {
    left: 30px;
    position: absolute;
    width: calc(100% - 30px);
  }

  &__text {
    font-size: 1rem;
  }
}

.icon-01d:before {
  content: "\f00d";
}

.icon-01n:before {
  content: "\f02e";
}

.icon-02d:before {
  content: "\f002";
}

.icon-02n:before {
  content: "\f086";
}

.icon-03d:before,
.icon-03n:before {
  content: "\f041";
}

.icon-04d:before,
.icon-04n:before {
  content: "\f013";
}

.icon-09d:before,
.icon-09n:before {
  content: "\f01a";
}

.icon-10d:before {
  content: "\f009";
}

.icon-10n:before {
  content: "\f037";
}

.icon-11d:before,
.icon-11n:before {
  content: "\f01e";
}

.icon-13d:before,
.icon-13n:before {
  content: "\f01b";
}

.icon-50d:before,
.icon-50n:before {
  content: "\f014";
}

/* converts icon font codes from simpleweather.js to Weather Icons from Erik Flowers */

// .icon-0:before {
//   content: "\f056";
// }
// .icon-1:before {
//   content: "\f03b";
// }
// .icon-2:before {
//   content: "\f01d";
// }
// .icon-3:before {
//   content: "\f01e";
// }
// .icon-4:before {
//   content: "\f01d";
// }
// .icon-5:before {
//   content: "\f017";
// }
// .icon-6:before {
//   content: "\f017";
// }
// .icon-7:before {
//   content: "\f017";
// }
// .icon-8:before {
//   content: "\f017";
// }
// .icon-9:before {
//   content: "\f01b";
// }
// .icon-10:before {
//   content: "\f017";
// }
// .icon-11:before {
//   content: "\f01b";
// }
// .icon-12:before {
//   content: "\f01b";
// }
// .icon-13:before {
//   content: "\f01b";
// }
// .icon-14:before {
//   content: "\f01b";
// }
// .icon-15:before {
//   content: "\f017";
// }
// .icon-16:before {
//   content: "\f01b";
// }
// .icon-17:before {
//   content: "\f017";
// }
// .icon-18:before {
//   content: "\f015";
// }
// .icon-19:before {
//   content: "\f014";
// }
// .icon-20:before {
//   content: "\f014";
// }
// .icon-21:before {
//   content: "\f014";
// }
// .icon-22:before {
//   content: "\f014";
// }
// .icon-23:before {
//   content: "\f014";
// }
// .icon-24:before {
//   content: "\f011";
// }
// .icon-25:before {
//   content: "\f011";
// }
// .icon-26:before {
//   content: "\f013";
// }
// .icon-27:before {
//   content: "\f031";
// }
// .icon-28:before {
//   content: "\f002";
// }
// .icon-29:before {
//   content: "\f031";
// }
// .icon-30:before {
//   content: "\f002";
// }
// .icon-31:before {
//   content: "\f02e";
// }
// .icon-32:before {
//   content: "\f00d";
// }
// .icon-33:before {
//   content: "\f02e";
// }
// .icon-34:before {
//   content: "\f00d";
// }
// .icon-35:before {
//   content: "\f017";
// }
// .icon-36:before {
//   content: "\f00d";
// }
// .icon-37:before {
//   content: "\f01d";
// }
// .icon-38:before {
//   content: "\f01d";
// }
// .icon-39:before {
//   content: "\f01d";
// }
// .icon-40:before {
//   content: "\f01a";
// }
// .icon-41:before {
//   content: "\f017";
// }
// .icon-42:before {
//   content: "\f01b";
// }
// .icon-43:before {
//   content: "\f017";
// }
// .icon-44:before {
//   content: "\f031";
// }
// .icon-45:before {
//   content: "\f01d";
// }
// .icon-46:before {
//   content: "\f015";
// }
// .icon-47:before {
//   content: "\f01d";
// }

              
            
!

JS

              
                let apiKey = "b949a98c4939fc9af1f13538f5467c2a";
let city = "portsmouth, uk";
let url = `https://api.openweathermap.org/data/2.5/forecast?q=${city}&appid=${apiKey}`;

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      error: null,
      isLoaded: false,
      days: [
        "Sunday",
        "Monday",
        "Tuesday",
        "Wednesday",
        "Thursday",
        "Friday",
        "Saturday"
      ],
      result: []
    };

    // this.handleClick = this.handleClick.bind(this);
  }

  componentDidMount() {
    fetch(url)
      .then(res => res.json())
      .then(
        result => {
          this.setState({
            isLoaded: true,
            result: result
          });
        },
        error => {
          this.setState({
            isLoaded: true,
            error
          });
        }
      );
  }

  handleClick(e) {
    e.preventDefault();
    let test = document.querySelectorAll(".weather__item.active");
    [].forEach.call(test, function(el) {
      el.classList.remove("active");
    });

    e.currentTarget.classList.toggle("active");
  }

  getWindDirection(degree) {
    const degreePerDirection = 360 / 8;
    const offsetAngle = degree + degreePerDirection / 2;

    return offsetAngle >= 0 * degreePerDirection &&
      offsetAngle < 1 * degreePerDirection
      ? "N"
      : offsetAngle >= 1 * degreePerDirection &&
        offsetAngle < 2 * degreePerDirection
        ? "NE"
        : offsetAngle >= 2 * degreePerDirection &&
          offsetAngle < 3 * degreePerDirection
          ? "E"
          : offsetAngle >= 3 * degreePerDirection &&
            offsetAngle < 4 * degreePerDirection
            ? "SE"
            : offsetAngle >= 4 * degreePerDirection &&
              offsetAngle < 5 * degreePerDirection
              ? "S"
              : offsetAngle >= 5 * degreePerDirection &&
                offsetAngle < 6 * degreePerDirection
                ? "SW"
                : offsetAngle >= 6 * degreePerDirection &&
                  offsetAngle < 7 * degreePerDirection
                  ? "W"
                  : "NW";
  }

  renderItem(item, index) {
    let temp = item.main.temp - 273.15;
    temp = Math.round(temp * 100 / 100);
    let date = new Date(item.dt_txt.substring(0, 10));
    let day = date.getDay();
    let windDirection = this.getWindDirection(item.wind.deg);
    let windSpeed = Math.round((item.wind.speed * 2.2369362920544) * 100/ 100);

    return (
      <li
        className={"weather__item " + (index === 0 ? "active" : false)}
        onClick={this.handleClick}
      >
        <div className="weather__icon">
          <i className={`wi icon-${item.weather[0].icon}`} />
        </div>
        <div className="weather__details">
          <div className="weather__day">{this.state.days[day]}</div>
          <div className="weather__info">
            <div className="weather__temp">{temp}&deg;C</div>
            <div className="weather__text">{item.weather[0].main}</div>
            <div className="weather__text">
              Wind: {windDirection} {windSpeed}mph
            </div>
            <div className="weather__text">
              Humidity: {item.main.humidity}%
            </div>
          </div>
        </div>
      </li>
    );
  }

  render() {
    const { error, isLoaded, result } = this.state;
    const items = result.list;
    // console.log(items);

    if (error) {
      return <div>Error: {error.message}</div>;
    } else if (!isLoaded) {
      return <div>Loading...</div>;
    } else {
      let filteredList = result.list.filter((item, index) => {
        if (
          index === 0 ||
          index === 8 ||
          index === 16 ||
          index === 24 ||
          index === 32
        ) {
          return item;
        }
      });

      // console.log(result.list);

      let listItems = filteredList.map((item, index) => {
        return this.renderItem(item, index);
      });

      return (
        <div class="weather" id="js-weather">
          <header class="weather__header">
            <div class="weather__city">
              <i class="fa fa-map-marker" />
              {result.city.name}
            </div>
            <div class="weather__country">{result.city.country}</div>
          </header>
          <ul class="weather__list">{listItems}</ul>
        </div>
      );
    }
  }
}

ReactDOM.render(<MyComponent />, document.getElementById("root"));

              
            
!
999px

Console