javascript

typescript
import React, { createContext, useContext, useEffect, useState, ReactNode } from 'react'

type Theme = 'light' | 'dark'

interface ThemeContextType {
  theme: Theme

Dark mode with Tailwind and localStorage persistence

react tailwind dark-mode
by Maya Patel 3 tabs
css
.confirm-dialog {
  border: none;
  border-radius: 12px;
  padding: 1.5rem;
  max-width: 26rem;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.25);

Stimulus: resilient confirmation for destructive actions

rails stimulus hotwire
by codesnips 3 tabs
erb
<div class="layout" data-controller="viewport">
  <%= turbo_frame_tag "master", class: "master-pane" do %>
    <h1>Products</h1>
    <ul class="product-list">
      <% @products.each do |product| %>
        <li>

Turbo Frames: conditional frame navigation for mobile vs desktop

rails hotwire responsive
by codesnips 4 tabs
ruby
class ApplicationController < ActionController::Base
  before_action :authenticate_user!

  rescue_from ActionController::InvalidAuthenticityToken do
    handle_unauthenticated(reason: :csrf)
  end

Turbo Streams: partial page auth failure handling

rails turbo hotwire
by codesnips 3 tabs
javascript
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  static targets = ["parent", "child"]
  static values = {
    options: Object,

Stimulus controller for dependent select dropdowns

stimulus javascript forms
by Jordan Lee 2 tabs
erb
<%= link_to 'Delete', task_path(task),
      data: { controller: 'confirm', action: 'confirm#ask', confirm_message_value: 'Delete this task permanently?', turbo_method: :delete },
      class: 'text-red-700' %>

Custom confirm dialog with Stimulus (better than window.confirm)

rails hotwire stimulus
by Henry Kim 2 tabs
erb
<%= turbo_frame_tag 'modal' %>

Keyboard shortcut “command palette” modal (Hotwire-first)

rails hotwire stimulus
by Henry Kim 3 tabs
javascript
import React, { lazy, Suspense, useState, useEffect } from 'react';

// 1. Component lazy loading
const HeavyComponent = lazy(() => import('./HeavyComponent'));
const AdminPanel = lazy(() => import('./AdminPanel'));
const Dashboard = lazy(() => import('./Dashboard'));

Performance optimization - lazy loading and code splitting

performance optimization lazy-loading
by Alex Chang 2 tabs
javascript
import React, { useState, useEffect, useCallback, useMemo, useRef, useContext } from 'react';

// 1. useState - managing component state
function Counter() {
  const [count, setCount] = useState(0);
  const [name, setName] = useState('');

React hooks - useState, useEffect, and custom hooks

react javascript hooks
by Alex Chang 1 tab
javascript
// Basic closure example
function outer() {
  const message = 'Hello from outer';

  function inner() {
    console.log(message); // Accesses outer variable

JavaScript closures and lexical scope fundamentals

javascript closures scope
by Alex Chang 1 tab
ruby
class ApplicationController < ActionController::Base
  before_action do
    Current.client = request.headers['X-Client']
  end
end

Attach custom headers to Turbo fetch requests (stimulus-free)

rails hotwire turbo
by Henry Kim 2 tabs
erb
<div data-controller="char-counter" data-char-counter-max-value="280">
  <%= form.text_area :body, rows: 6, data: { char_counter_target: 'input' }, class: 'w-full rounded border p-2' %>
  <div class="mt-1 text-sm text-gray-500">
    <span data-char-counter-target="output"></span> characters remaining
  </div>
</div>

Character counter for textareas with Stimulus

rails hotwire stimulus
by Henry Kim 2 tabs