Class: TailwindTheme::Theme

Inherits:
Object
  • Object
show all
Defined in:
lib/tailwind_theme.rb

Overview

The Tailwind CSS Theme object

Examples:

TailwindTheme::Theme.new({ button: "rounded p-8 bg-blue-600 text-white hover:bg-blue-500" })

Constant Summary collapse

BASE_KEY =

The base key name for the sub theme

"base"
NIL_KEY =

The key to use for a nil value

"nil"

Instance Method Summary collapse

Constructor Details

#initialize(theme_hash = {}) ⇒ Theme

Returns a new instance of Theme.



65
66
67
# File 'lib/tailwind_theme.rb', line 65

def initialize(theme_hash = {})
  @theme = theme_hash
end

Instance Method Details

#[](path) ⇒ String, ...

Lookup the raw value of the path

Parameters:

  • path (String, Symbol, Array[String, Symbol])

    the path to the css classes

Returns:

  • (String, Hash, NilClass)


162
163
164
165
# File 'lib/tailwind_theme.rb', line 162

def [](path)
  path = normalize_path path
  lookup_path path, raise: false
end

#css(path, options = {}) ⇒ String

Get the merged Tailwind CSS classes

Default options are:

:raise => false

Parameters:

  • path (String, Symbol, Array<String, Symbol>)

    the path to the css classes or sub theme

  • options (Hash<Symbol, Object>) (defaults to: {})

    the options to use when parsing the css theme

Options Hash (options):

  • prepend (prepend)

    the classnames before merging the Tailwind Classes

  • append (append)

    the classnames before merging the Tailwind Classes

  • raise (Boolean)

    raise an ‘IndexError` if the `path` does not exist

  • object (Object)

    the object to apply the sub theme

  • attributes (Hash<String, Object>)

    the attributes to apply the sub theme. Overrides the attributes defined in ‘options`.

Returns:

  • (String)

    the merged Tailwind CSS classes

Raises:

  • (IndexError)

    if the :raise options is true and the path cannot be found

  • (ArgumentError)

    if processing an object theme and the object or attributes option is not defined



87
88
89
90
# File 'lib/tailwind_theme.rb', line 87

def css(path, options = {})
  classnames = build path, options
  merge classnames, options
end

#css!(path, options = {}) ⇒ String

Get the merged Tailwind CSS classes. Raises an IndexError if the path cannot be found.

Parameters:

  • path (String, Symbol, Array<String, Symbol>)

    the path to the css classes or sub theme

  • options (Hash<Symbol, Object>) (defaults to: {})

    the options to use when parsing the css theme

Options Hash (options):

  • prepend (prepend)

    the classnames before merging the Tailwind Classes

  • append (append)

    the classnames before merging the Tailwind Classes

  • object (Object)

    the object to apply the sub theme

  • attributes (Hash<String, Object>)

    the attributes to apply the sub theme. Overrides the attributes defined in ‘options`.

Returns:

  • (String)

    the merged Tailwind CSS classes

Raises:

  • (IndexError)

    if the :raise options is true and the path cannot be found

  • (ArgumentError)

    if processing an object theme and the object or attributes option is not defined



106
107
108
# File 'lib/tailwind_theme.rb', line 106

def css!(path, options = {})
  css path, options.merge(raise: true)
end

#key?(path) ⇒ Boolean

Returns if the path exists

Returns:

  • (Boolean)

    returns true if the path exists



154
155
156
157
# File 'lib/tailwind_theme.rb', line 154

def key?(path)
  path = normalize_path path
  !!lookup_path(path, raise: false)
end

#merge_css(paths, options = {}) ⇒ String

Combine multiple paths and merging the combined Tailwind CSS classes.

Default options are:

:raise => false

Parameters:

  • paths (Array<String, Symbol, Array<String, Symbol>>)

    the array of paths to combine

  • options (Hash<Symbol, Object>) (defaults to: {})

    the options to use when parsing the css theme

Options Hash (options):

  • prepend (prepend)

    the classnames before merging the Tailwind Classes

  • append (append)

    the classnames before merging the Tailwind Classes

  • raise (Boolean)

    raise an ‘IndexError` if the a path does not exist

  • object (Object)

    the object to apply the sub theme

  • attributes (Hash<String, Object>)

    the attributes to apply the sub theme. Overrides the attributes defined in ‘options`.

Returns:

  • (String)

    the merged Tailwind CSS classes

Raises:

  • (IndexError)

    if the :raise options is true and a path cannot be found

  • (ArgumentError)

    if processing an object theme and the object or attributes option is not defined



128
129
130
131
# File 'lib/tailwind_theme.rb', line 128

def merge_css(paths, options = {})
  classnames = paths.map { |path| build path, options }.compact.join(" ")
  merge classnames, options
end

#merge_css!(paths, options = {}) ⇒ String

Combine multiple paths and merging the combined Tailwind CSS classes. Raises an IndexError if a path

cannot be found.

Parameters:

  • paths (Array<String, Symbol, Array<String, Symbol>>)

    the array of paths to combine

  • options (Hash<Symbol, Object>) (defaults to: {})

    the options to use when parsing the css theme

Options Hash (options):

  • prepend (prepend)

    the classnames before merging the Tailwind Classes

  • append (append)

    the classnames before merging the Tailwind Classes

  • object (Object)

    the object to apply the sub theme

  • attributes (Hash<String, Object>)

    the attributes to apply the sub theme. Overrides the attributes defined in ‘options`.

Returns:

  • (String)

    the merged Tailwind CSS classes

Raises:

  • (IndexError)

    if the :raise options is true and a path cannot be found

  • (ArgumentError)

    if processing an object theme and the object or attributes option is not defined



148
149
150
# File 'lib/tailwind_theme.rb', line 148

def merge_css!(paths, options = {})
  merge_css paths, options.merge(raise: true)
end