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)


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

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):

  • 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



85
86
87
88
# File 'lib/tailwind_theme.rb', line 85

def css(path, options = {})
  classnames = build path, options
  merge classnames
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):

  • 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



102
103
104
# File 'lib/tailwind_theme.rb', line 102

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



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

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):

  • 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



122
123
124
125
# File 'lib/tailwind_theme.rb', line 122

def merge_css(paths, options = {})
  classnames = paths.map { |path| build path, options }.compact.join(" ")
  merge classnames
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):

  • 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



140
141
142
# File 'lib/tailwind_theme.rb', line 140

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