npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

styled-logs

v0.1.9

Published

Styled console statements with modern JavaScript

Downloads

34

Readme

Styled-Logs Icon

styled-logs

Styled console statements with modern JavaScript

npm version npm NPM npm bundle size

Zero dependencies

Written in TypeScript (types included)

Inspired by styled-components

Styled console statements are an experimental feature with limited browser support and even more limited documentation. Style rules do not always work as expected. Due to these limitations, results may vary. Please use with caution.

Installation

yarn add styled-logs or npm i styled-logs

Basic Example

import styled from "styled-logs";

const Log = styled.log`
  color: purple;
  background: yellow;
`;

Log("I am a styled-log!");

Usage

Create your styled-log

This is done using a tagged templated literal to write your CSS styles.

import styled from "styled-logs";

const Log = styled.log`
  color: purple;
  background: yellow;
  border: 2px solid purple;
  padding: 5px;
`;

The styled method exposes three of the global console methods as properties of the method:

log, warn, and error

const ErrorLog = styled.error`
  color: tomato;
  font-weight: bold;
`;

The latter three methods provide icons and additional styling provided by the browser:

Browser Console screenshot

Use your styled-log

This is done using a tagged templated literal to write your CSS styles. The styled method exposes three of the global console methods as properties of the method:

Log("🎉 Yay!");

You can optionally use a tagged template literal, which can be used to pass dynamic values.

let name = "LIJS";
Log`Hello ${name}!`;

// This also works
Log(`Hello ${name}!`);

Extend your styled-log

You can also create a styled-log by extending from the styles of a previously defined instance. You do this by passing a previously defined instance to the styled method as an argument followed by your new styles in a tagged template literal. The styles will be merged and in the event of a conflict, the latest style will always be applied.

const Log = styled.log`
  color: "purple";
`;

const ExtendedLog = styled(Log)`
  background: yellow;
`;

// ExtendedLog will have purple text
// with a yellow background.
ExtendedLog("🎉 Yay...extended!");

Creating Dynamic Styles

Adding dynamic styles is simple. You can include variables or expressions anywhere in your style declaration as long as they return a properly formatted string.

const error = false;

const Log = styled.log`
  color: ${error ? "tomato" : "green"};
`;

const Log2 = styled.log`
  color: green;
  ${() => show && `background: yellow`};
`;

const log3 = styled.log`
  color: green;
  background: ${() => show && `purple`};
`;

Misc.

As if the syntax above is not weird enough, you can also define and execute a styled-logs method in a one-liner.

styled.log`
  color: green;
`("I am a green console statement!");

Syntax

When defining your CSS styles, it's important to remember to use proper CSS syntax, which means finishing each declaration with a semicolon. This may not break your code in a simple situation, but you can definitely expect to see issues when trying to extend from styles or resolve expressions that are not formatted properly.

// This may not break despite the missing
// semicolon on the "color:blue" declaration.

const FirstLog = styled.log`
  color: blue;
`;

// But, this will definitely break (silently).
const SecondLog = styled(FirstLog)`
  border: 2px solid blue;
`;

Syntax Highlighting

Any syntax highlighting extension that works for styled-components should work for this library as well.

If you have never invoked a function with a template literal, it may look a little strange. log, warn and error are all methods of the styled object, but we are invoking them using the template literal syntax:

styled.log`
  color: blue;
`;

Turns out, this is a completely valid way of executing a JavaScript function. We are tagging the template literal (string) with a function that is able to parse the string and any of its expressions (and variables). The tag function, when invoked with a template literal, will receive as arguments the template literal broken out into an array of strings along with all of its individual expressions and variables in sequential order.

Learn more about tagged template literals from MDN or styled-components.

Enjoy!