Semantic UI React
repository·master·Indexed 11 days ago
https://github.com/semantic-org/semantic-ui-reactThe 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.
What's inside Semantic UI React
- Because Semantic UI React is purely a component library that outputs Semantic UI-compatible HTML, you can load any Semantic UI CSS theme on top of your application to provide styling.
Pass unhandled props and DOM attributes through the `as` prop
masterSemantic 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 theasprop. This allows you to use standard HTML attributes (liketype,id,aria-*) or props from third-party libraries (likereact-router'sto) 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>Understand and use Shorthand Props
masterSemantic UI React components use "shorthands" to simplify the declaration of sub-elements. A shorthand prop (like
iconon aButton) 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' />Bundler Compatibility
masterSemantic UI React is compatible with modern JavaScript bundlers:
- Create React App: Works out of the box.
- Webpack 4/5: Fully supported. It is recommended to build your app in production mode before release to benefit from optimizations like stripping
propTypesfrom the build.
Compose components using the `as` prop
masterSemantic UI React components support an
asprop 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 theasprop (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' />Theming Fundamentals in Semantic UI React
masterSemantic 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.Handle `Popup` wrapping element and zIndex
masterStarting in v2,Popupuses an additional wrapping element for positioning. To pass props to this internal element, use thepoppershorthand. Note thatzIndexvalues are automatically transferred to this element to prevent layout breaks.Replace the removed `Responsive` component
masterThe
Responsivecomponent 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> </> );Theme customization with Create React App (CRA)
masterTo customize styles in a Create React App project without using
eject, use@craco/cracoand@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-css2. 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-less3. 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/bootstrap6. Configure theme.config
Ensure your
src/semantic-ui/theme.configpoints 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'Scaffold custom styles manually
masterIf you prefer not to use the
@semantic-ui-react/bootstrapCLI tool, you can manually scaffold your customization by copying files fromnode_modulesto yoursrcdirectory:- Copy the entire
node_modules/semantic-ui-less/_sitefolder tosrc/semantic-ui/site. - Copy
node_modules/semantic-ui-less/theme.config.exampletosrc/semantic-ui/theme.config.
- Copy the entire
Replace the `Visibility` component
masterThe
Visibilitycomponent has been removed in v3 to improve performance. It is recommended to use the native Intersection Observer API directly or use a React wrapper likereact-intersection-observer.For a smoother transition, you can use the
@semantic-ui-react/component-visibilitypackage, but it is considered deprecated and is not actively maintained.-import { Visibility } from "semantic-ui-react"; +import Visibility from "@semantic-ui-react/component-visibility";Migrate from the `Ref` component to native refs
masterThe
Refcomponent has been removed in v3 because native ref support makes it obsolete. To upgrade, replace the<Ref>wrapper with therefprop directly on the target component.If you need a temporary bridge during migration, you can use the
@semantic-ui-react/component-refpackage, 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";