Skip to content
⚠️ This article was written in 2019. Some content may be outdated.

CSS Print Stylesheet Solutions

CSS print stylesheets are a common challenge in day-to-day development. Drawing on real projects, this article shares concrete implementation approaches and lessons learned.

Basic Concepts ​

Let's start with the basic usage:

css
function deepClone(obj, map = new WeakMap()) {
  if (obj === null || typeof obj !== 'object') return obj
  if (map.has(obj)) return map.get(obj)

  const clone = Array.isArray(obj) ? [] : {}
  map.set(obj, clone)

  for (const key of Object.keys(obj)) {
    clone[key] = deepClone(obj[key], map)
  }
  return clone
}

This approach is clean and clear, and works well for most scenarios.

Deep Dive ​

The core code looks like this:

css
function pLimit(concurrency) {
  const queue = []
  let active = 0

  const next = () => {
    if (active >= concurrency || queue.length === 0) return
    active++
    const { fn, resolve, reject } = queue.shift()
    fn().then(resolve, reject).finally(() => {
      active--
      next()
    })
  }

  return (fn) => new Promise((resolve, reject) => {
    queue.push({ fn, resolve, reject })
    next()
  })
}

In real projects you also need to consider edge cases and error handling.

Project Application ​

Here is a concrete example:

css
class EventEmitter {
  constructor() {
    this.events = new Map()
  }

  on(event, handler) {
    if (!this.events.has(event)) {
      this.events.set(event, [])
    }
    this.events.get(event).push(handler)
    return () => this.off(event, handler)
  }

  off(event, handler) {
    const handlers = this.events.get(event)
    if (handlers) {
      const idx = handlers.indexOf(handler)
      if (idx > -1) handlers.splice(idx, 1)
    }
  }

  emit(event, ...args) {
    const handlers = this.events.get(event) || []
    handlers.forEach(h => h(...args))
  }
}

After rolling this pattern out across the team, the results were positive and maintenance cost dropped noticeably.

Common Issues ​

We can implement it in the following way:

css
interface User {
  id: number
  name: string
  email: string
  role: 'admin' | 'user' | 'guest'
}

function createUser(data: Partial<User>): User {
  return {
    id: Date.now(),
    name: data.name || '',
    email: data.email || '',
    role: data.role || 'user'
  }
}

type UserKeys = keyof User  // 'id' | 'name' | 'email' | 'role'

Pay attention to the performance details in the code above and avoid unnecessary computation.

Summary ​

  • The key to CSS print stylesheets is understanding the core concepts, not just the surface-level usage
  • Pick the right approach for the scenario in real projects
  • Consistent team conventions matter more than chasing a perfect implementation
  • Keep learning and summarizing to stay technically sharp

MIT Licensed