Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <div id="root"></div>
              
            
!

CSS

              
                body {
  display: grid;
  place-content: center;
  height: 100vh;
}

#root {
  > * + * {
    margin-top: 0.5rem;
  }
}

              
            
!

JS

              
                import React, {
  useState,
  ReactNode,
  useCallback,
  KeyboardEvent,
  MouseEvent,
  HTMLAttributes,
  HTMLButtonElement
} from "https://esm.sh/react@18";
import { createRoot } from "https://esm.sh/react-dom@18";

type ButtonProps = {
  onClick?: (e: MouseEvent) => void;
  children?: ReactNode;
  className?: string;
  disabled?: boolean;
} & HTMLAttributes<HTMLButtonElement>;

const Button = ({
  onClick,
  children,
  className = "",
  disabled = false,
  ...props
}: ButtonProps) => {
  const handleKeyDown = useCallback(
    (e: KeyboardEvent) => {
      if (disabled) return;

      if (e.key === " " || e.key === "Enter") {
        e.preventDefault();
        onClick?.((e as unknown) as MouseEvent);
      }
    },
    [onClick, disabled]
  );

  const handleClick = (e: MouseEvent) => {
    if (disabled) return;
    onClick?.(e);
  };

  return (
    <div
      {...props}
      tabIndex={disabled ? -1 : 0}
      onClick={handleClick}
      onKeyDown={handleKeyDown}
      aria-disabled={disabled}
      className={`
        inline-block px-4 py-2
        text-sm
        bg-blue-500 text-white
        rounded cursor-pointer select-none
        transition-all duration-200

        /* ホバー時のスタイル */
        hover:bg-blue-600

        /* フォーカス時のスタイル */
        focus:outline-none
        focus:ring-2
        focus:ring-blue-400
        focus:ring-offset-2

        /* アクティブ時のスタイル */
        active:bg-blue-700

        /* 非活性時のスタイル */
        ${
          disabled &&
          `
          bg-gray-200
          text-gray-400
          cursor-not-allowed
          hover:bg-gray-200
          hover:shadow-none
          focus:ring-0
          focus:ring-offset-0
          pointer-events-none
          opacity-80
        `
        }
      `}
    >
      {children}
    </div>
  );
};

const App = () => {
  return (
    <>
      <div>
        <Button onClick={() => alert("Clicked!")}>Pure Div Button</Button>
      </div>
      <div>
        <Button disabled onClick={() => alert("Clicked!")}>
          Pure Div Button (disabled)
        </Button>
      </div>
    </>
  );
};

const rootElement = document.getElementById("root");
if (rootElement) {
  const root = createRoot(rootElement);
  root.render(<App />);
}

              
            
!
999px

Console