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

              
                <nav>
  <div class="top-nav">
    <div class="country-selection">
      <div>Change Country</div>
      <div class="country-list">
        <div class="country" id="au">Australia</div>
        <div class="country" id="ar">Argentina</div>
        <div class="country" id="be">Belgium</div>
        <div class="country" id="br">Brazil</div>
        <div class="country" id="ca">Canada</div>
        <div class="country" id="cn">China</div>
        <div class="country" id="fr">France</div>
        <div class="country" id="de">Germany</div>
        <div class="country" id="in">India</div>
        <div class="country" id="jp">Japan</div>
        <div class="country" id="kr">Korea</div>
        <div class="country" id="mx">Mexico</div>
        <div class="country" id="nz">New Zealand</div>
        <div class="country" id="ng">Nigeria</div>
        <div class="country" id="pt">Portugal</div>
        <div class="country" id="za">South Africa</div>
        <div class="country" id="es">Spain</div>
        <div class="country" id="gb">United Kingdom</div>
        <div class="country" id="us">United States</div>
      </div>
    </div>
    <div class="logins">
      <button class="btn-subscribe">SUBSCRIBE NOW</button>
      <button class="btn-login">LOG IN</button>
    </div>
  </div>
  <div class="main-title">
    <div class="logo">WORLD NEWS BROWSER</div>
    <div class="info">
      <div class="info-child date">
      </div>
      <div class="info-child weather">
        <div class="weather-temp"></div>
      </div>
      <div class="info-child weather-img-holder">
        <img class="weather-img" src="" alt="">
      </div>
    </div>
  </div>
  <hr>
  <div class="topic-links">
    <div class="topic">Business</div>
    <div class="topic">Entertainment</div>
    <div class="topic">General</div>
    <div class="topic">Health</div>
    <div class="topic">Science</div>
    <div class="topic">Sports</div>
    <div class="topic">Technology</div>
  </div>
</nav>
<div id="cardHolder">
</div>
<a class="attribution" target="_blank" href="https://newsapi.org">
  News Search Results Powered by newsAPI
</a>
              
            
!

CSS

              
                $mainColour: #334098;
nav {
  font-family: Lato, sans-serif;
  display: flex;
  flex-direction: column;
  .top-nav,
  .topic-links {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
  }
  .top-nav {
    padding: 10px;
    button {
      padding: 5px 5px;
      font-size: 11px;
      font-weight: 700;
      margin-right: 5px;
      color: #fff;
      background-color: $mainColour;
      border: none;
      border-radius: 4px;
      padding: 10px 10px;
      cursor: pointer;
      &:focus {
        outline: none;
      }
    }
    .country-selection {
      background-color: $mainColour;
      color: white;
      cursor: pointer;
      padding: 10px 15px;
      &.active {
        .country-list {
          transition: all 0.5s;
          transform: scale(1);
        }
      }
      .country-list {
        z-index: 2;
        position: absolute;
        background-color: #333;
        color: white;
        margin-top: 10px;
        margin-left: -15px;
        padding: 2px 5.5px;
        position: absolute;
        transform-origin: top;
        transition: all .2s;
        transform: scale(1, 0);
        .country {
          padding: 10px;
          border-top: 1px solid white;
        }
      }
    }
  }
  .main-title {
    .logo {
      font-family: "PT Serif", serif;
      font-size: 40px;
      display: block;
      text-align: center;
    }
    .info {
      display: flex;
      justify-content: center;
      .weather {
        display: flex;
      }
      .weather-img-holder {
        margin-top: -11px;
        margin-left: 10px;
        img {
          width: 40px;
          height: 40px;
        }
      }
      .info-child {
        padding: 10px;
      }
    }
  }
  hr{
    display:block;
    width: 95%;
    border: .5px solid black;
}
  .topic-links {
    padding: 20px 100px;
    cursor: pointer;
    .topic{
      padding: 10px 5px;
      border-radius: 5px;
      &.active{
        border: 1px solid $mainColour;
      }
    }
  }
}

#cardHolder {
  display: flex;
  flex-flow: wrap;
  justify-content: center;
  flex-basis: 400px;
  .newsCard {
    width: 300px;
    height: 450px;
    margin: 50px 10px;
    background-color: #eee;
    position: relative;
    box-shadow: 0px 2px 5px 1px rgba(0, 0, 0, 0.2);
    border-radius: 50px 5px 50px 5px;
    img {
      background-color: black;
      position: relative;
      width: 100%;
      height: 40%;
      left: 50%;
      transform: translateX(-50%);
      margin-bottom: 10px;
      border-radius: 50px 5px 0px 0px;
    }
    .title {
      font-family: "PT Serif", serif;
      cursor: pointer;
      padding: 10px;
      height: auto;
      height: 54px;
      overflow: hidden;
      margin-bottom: 10px;
      font-weight: 700;
      font-size: 16px;
      text-align: center;
      a{
        font-size: 16px;
        font-weight: 700;
        text-decoration: none;
        color: $mainColour;
      }
    }
    .description {
      height: 75px;
      overflow: hidden;
      padding: 20px 20px;
      padding-top: 0px;
      margin-bottom: 20px;
      padding-top: 0px;
      font-family: Lato, sans-serif;
    }
    .read-more{
          a {
      text-decoration: none;
      color: white;
      background-color: #334098;
      border-radius: 5%;
      font-family: Lato, sans-serif;
      padding: 10px 5px;
      left: 50%;
      position: absolute;
      transform: translateX(-50%);
    }
    }
  }
}

.attribution {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
}

              
            
!

JS

              
                document.querySelector(".country-selection").addEventListener("click", () => {
  let country = document.querySelector(".country-selection");
  if (country.classList.contains("active")) {
    country.classList.remove("active");
  } else {
    country.classList.add("active");
  }
});

//Get and Show Today's weather based on latitude and longitutde
let getWeather = (lat, long, farenheit = true) => {
  return fetch(
    `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${long}&appid=9af05c564f8048803d0ce5cbfef989af`
  )
    .then(res => {
      if (res.status === 200) {
        return res.json();
      } else {
        throw res.status;
      }
    })
    .then(res => {
      console.log(res);
      //Weather Icon
      let weatherIcon = `http://openweathermap.org/img/w/${
        res.weather[0].icon
      }.png`;
      // Temperature in Farenheit or Celsius
      let temp;
      if (farenheit) {
        temp = res.main.temp * (9 / 5) - 459.67;
      } else {
        temp = res.main.temp - 273.15;
      }
      temp = temp.toFixed(1);
      return {
        weatherIcon,
        temp
      };
    })
    .catch(err => {
      return 1;
    });
};

let setWeatherWithLocation = () => {
  // Check if geolocation is turned on in user's browser
  if ("geolocation" in navigator) {
    navigator.geolocation.getCurrentPosition(position => {
      //Use getWeather to get the weather object with icon & temperature in celsius
      getWeather(
        position.coords.latitude,
        position.coords.longitude,
        false
      ).then(res => {
        document.querySelector(".weather-temp").innerHTML = `${
          res.temp
        } &#8451`;
        document.querySelector(".weather-img").src = res.weatherIcon;
      });
    }, error => 1);
  } else {
    //if no geolocation, return 1
    console.log("no geolocation");
    return 1;
  }
};

let setInfo = () => {
  /*
    DATE SECTION
  */
  let months = [
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"
  ];
  let days = [
    "Sunday",
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday"
  ];
  let date = new Date();
  let year = date.getFullYear();
  let day_number = date.getDate();
  let day_string = days[date.getDay()];
  let month = months[date.getMonth()];
  document.querySelector(
    ".date"
  ).innerText = `${day_string} ${month} ${day_number} ${year}`;

  /*
    WEATHER SECTION
  */
  setWeatherWithLocation();
};

setInfo();
/*
  Start of News Section
*/

/*
  Main News Component - Parent
  Checks for changes to country and category, passes the values down to NewsCardHandler component
*/

class MainNewsComp extends React.Component {
  constructor(props) {
    super(props);
    this.state = { country: "US", category: "Health" };
  }
  componentDidMount() {
    document.querySelectorAll(".topic").forEach(node =>
      node.addEventListener("click", () => {
        this.setState(prevState => ({
          ...prevState,
          category: node.innerText
        }));
        //Removes *active* class from all topic nodes
        document.querySelectorAll(".topic").forEach(node2 => {
          if (node2.classList.contains("active")) {
            node2.classList.remove("active");
          }
        });
        //Adds *active* class to currently clicked node
        node.classList.add("active");
      })
    );
    document.querySelectorAll(".country").forEach(node =>
      node.addEventListener("click", () => {
        this.setState(prevState => ({
          ...prevState,
          country: node.id
        }));
      })
    );
  }
  render() {
    return (
      <NewsCardsHandler
        country={this.state.country}
        category={this.state.category}
      />
    );
  }
}

/*
   News Cards Handler
   Calls news api, and sends the returned array through to NewsCard component once promise resolves
*/

class NewsCardsHandler extends React.Component {
  constructor(props) {
    super(props);
    this.state = { arr: [{ title: "loading" }] };
  }
  getURL(country, category) {
    let url = `https://newsapi.org/v2/top-headlines?country=${country}&category=${category}&apiKey=283ea64ae31249498b803c3022a7210e`;
    fetch(url)
      .then(res => {
        if (res.status === 200) {
          return res;
        } else {
          throw res;
        }
      })
      .then(res => res.json())
      .then(res => res.articles)
      .then(res => {
        console.log(res);
        this.setState(() => ({ arr: res }));
      })
      .catch(err => console.log(err));
  }
  componentDidMount() {
    this.getURL(this.props.country, this.props.category);
  }
  componentWillReceiveProps(newProps) {
    this.getURL(newProps.country, newProps.category);
  }
  render() {
    return <NewsCards arr={this.state.arr} />;
  }
}
/*
  News Cards Component
  UI component --> 
*/
const NewsCards = props =>
  props.arr.map(item => (
    <div className="newsCard">
      <img src={item.urlToImage} />
      <div className="title"><a href={item.url} target="_blank">{item.title}</a></div>
      <div className="description">{item.description}</div>
      <div className="read-more"><a target="_blank" href={item.url}>
        {item.url ? "Read Article" : false}
        </a></div>
    </div>
  ));

ReactDOM.render(<MainNewsComp />, document.getElementById("cardHolder"));

              
            
!
999px

Console