Semantic UI React

repository·master·Indexed 11 days ago

https://github.com/semantic-org/semantic-ui-react

The official React integration for the Semantic UI design system, providing declarative, accessible, and keyboard-supported components that render valid Semantic UI HTML. Version 3.0.0-beta.2 introduces native ref forwarding and removes the Ref and Visibility components. The library supports component composition via the 'as' prop and simplified sub-element declaration through shorthand props.

Tokens
12.3K
Snippets
70
Records
81
Agent score
96%

What's inside Semantic UI React

  1. Pass unhandled props and DOM attributes through the `as` prop

    master

    Semantic UI React components only handle props defined in their propTypes. Any other props (unhandled props) are automatically passed down to the element or component specified in the as prop. This allows you to use standard HTML attributes (like type, id, aria-*) or props from third-party libraries (like react-router's to) on Semantic UI React components.

    // Passing a standard HTML attribute through to the underlying <button>
    <Button type='submit' />
    
    // Passing a third-party prop (to) through to a React Router Link
    import { Link } from 'react-router-dom'
    import { Button } from 'semantic-ui-react'
    
    <Button as={Link} to='/home'>
      To homepage
    </Button>
  2. Understand and use Shorthand Props

    master

    Semantic UI React components use "shorthands" to simplify the declaration of sub-elements. A shorthand prop (like icon on a Button) acts as a recipe that is eventually evaluated into a React Element. You can provide various types of values—strings, objects, or React elements—to customize how these sub-elements are rendered.

    import { Button } from 'semantic-ui-react'
    
    // Basic shorthand usage
    <Button icon='like' />
  3. Compose components using the `as` prop

    master

    Semantic UI React components support an as prop that allows you to change the underlying HTML element or React component being rendered. This enables component composition without adding extra nesting in your DOM tree. Every component has a default value for the as prop (usually the semantic HTML equivalent), but you can override it with any valid HTML tag or React component.

    {/* Default behavior: outputs <button class='ui button' /> */}
    <Button />
    
    {/* Overridden behavior: outputs <a class='ui button' /> */}
    <Button as='a' />
  4. Theming Fundamentals in Semantic UI React

    master
    Semantic UI React does not have its own styling system. Instead, it relies entirely on the theming of Semantic UI. You can customize the appearance by updating LESS variables or using predefined themes. All theming and styling concepts applicable to Semantic UI are directly applicable to Semantic UI React.
  5. Replace the removed `Responsive` component

    master

    The Responsive component was removed in v2 because it lacked proper Server Side Rendering (SSR) support and a direct connection to breakpoints. To achieve responsive behavior that works with SSR (e.g., in Next.js or Gatsby), use @artsy/fresnel.

    import { createMedia } from "@artsy/fresnel";
    import React from "react";
    import { Segment } from "semantic-ui-react";
    
    const AppMedia = createMedia({
      breakpoints: {
        mobile: 320,
        tablet: 768,
        computer: 992,
        largeScreen: 1200,
        widescreen: 1920
      }
    });
    const mediaStyles = AppMedia.createMediaStyle();
    const { Media, MediaContextProvider } = AppMedia;
    
    const App = () => (
      <>
        <style>{mediaStyles}</style>
        <MediaContextProvider>
          <Segment as={Media} at="mobile">
            Mobile
          </Segment>
        </MediaContextProvider>
      </>
    );
  6. Theme customization with Create React App (CRA)

    master

    To customize styles in a Create React App project without using eject, use @craco/craco and @semantic-ui-react/craco-less. This allows you to add LESS support to your build process while maintaining the ability to update CRA easily.

    ### 1. Remove existing styles
    ```bash
    npm uninstall semantic-ui semantic-ui-css
    # or
    yarn remove semantic-ui semantic-ui-css

    2. Install required dependencies

    npm install @craco/craco @semantic-ui-react/craco-less --save-dev
    npm install semantic-ui-less --save
    # or
    yarn add @craco/craco @semantic-ui-react/craco-less --dev
    yarn add semantic-ui-less

    3. Update package.json

    {
      "scripts": {
        "start": "craco start",
        "build": "craco build",
        "test": "craco test",
        "eject": "craco eject"
      }
    }

    4. Create craco.config.js

    module.exports = {
      plugins: [{ plugin: require('@semantic-ui-react/craco-less') }],
    }

    5. Scaffold custom styles

    Use the bootstrap CLI tool to copy the necessary skeleton files:

    npx @semantic-ui-react/bootstrap

    6. Configure theme.config

    Ensure your src/semantic-ui/theme.config points to the correct folders:

    @themesFolder : 'themes';
    @siteFolder  : '../../src/semantic-ui/site';
    
    @import (multiple) '~semantic-ui-less/theme.less';
    @fontPath : '../../../themes/@{theme}/assets/fonts';

    7. Import the main LESS file

    In your entry file (e.g., index.js), import the main LESS file:

    import 'semantic-ui-less/semantic.less'
  7. Scaffold custom styles manually

    master

    If you prefer not to use the @semantic-ui-react/bootstrap CLI tool, you can manually scaffold your customization by copying files from node_modules to your src directory:

    1. Copy the entire node_modules/semantic-ui-less/_site folder to src/semantic-ui/site.
    2. Copy node_modules/semantic-ui-less/theme.config.example to src/semantic-ui/theme.config.
  8. Replace the `Visibility` component

    master

    The Visibility component has been removed in v3 to improve performance. It is recommended to use the native Intersection Observer API directly or use a React wrapper like react-intersection-observer.

    For a smoother transition, you can use the @semantic-ui-react/component-visibility package, but it is considered deprecated and is not actively maintained.

    -import { Visibility } from "semantic-ui-react";
    +import Visibility from "@semantic-ui-react/component-visibility";
  9. Migrate from the `Ref` component to native refs

    master

    The Ref component has been removed in v3 because native ref support makes it obsolete. To upgrade, replace the <Ref> wrapper with the ref prop directly on the target component.

    If you need a temporary bridge during migration, you can use the @semantic-ui-react/component-ref package, but note that this package is deprecated and will not receive updates for React API changes.

    function App() {
    -  return (
    -    <Ref innerRef={buttonRef}>
    -      <Button />
    -    </Ref>
    -  )
    +  return <Button ref={buttonRef} />
    }
    -import { Ref } from "semantic-ui-react";
    +import Ref from "@semantic-ui-react/component-ref";